1. If you're receiving a message that you are banned from the Current Events or Politics forums, it's not you specifically: those forums have been hidden for all users. For more info, see here.
    Dismiss Notice

Pablo's Powershell Pow-Wow

Discussion in 'Business & Enterprise Computing' started by PabloEscobar, Sep 18, 2014.

  1. Dre_

    Dre_ Member

    Joined:
    May 25, 2014
    Messages:
    841
    Awesome!

    How do people manage their library of scripts and share them efficiently across a team of people?
    Ideally a central place where we can update the scripts easily.
     
  2. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,169
    Location:
    Brisbane
    Any place I've worked in where this has worked well has been via a private Git repository.

    Other places I've worked where it hasn't worked have been just using a file share...
     
  3. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,507
    Location:
    Melbourne
    Previously, I have had a central server which kept the Scripts, Logs and any scheduled tasks that run

    At my current company, its all over the show, and Im trying to sort out how to wrangle it in now
     
  4. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,610
    Source control.

    Git or Subversion, Keeps everything in a single place, is handy for version control, and seeing what people have done that have broken other stuff :).
     
  5. Dre_

    Dre_ Member

    Joined:
    May 25, 2014
    Messages:
    841
    With Git I presume I can have a script that shows a list of scripts and can select them to run or something?

    Englush not so good with lack of sleep lah today
     
  6. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,610
    Not really. It can be married to a system like that, but Git is just version control, so you check out your script as you are editing it, and then you check it back in with changes... so you have historical versions of all your scripts.
     
  7. Dre_

    Dre_ Member

    Joined:
    May 25, 2014
    Messages:
    841
    Any ideas on where I'd need to look for that?

    Want to keep it simple of people to run the scripts on demand, not wanting them to update them.
     
  8. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,507
    Location:
    Melbourne
    Quick question for everyone.

    We currently have a script that imports user info into AD from a csv

    I have been asked to add a lookup in the script.

    We have another csv which has list of Offices and matching site names

    We need the script to see if a field from the first csv matches the Office then it returns the site name to be added to the users account

    any ideas on how to go about this?
     
  9. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,610
    Nested Loops are how I currently do this. I 'feel' like its a bad way of doing it, especially if you are making a slow call as part of one of the inner loops, but I'm a sysadmin, not a coder, so I don't know any better.

    OfficeSites.csv
    Code:
    Site,Office
    NSW,NSWOffice
    SA,SAOffice
    Vic,VicOffice
    
    Userlist.csv
    Code:
    Username,Name,Office
    Bazza,Barry,NSWOffice
    SABazza,SABarry,SAOffice
    VicBazza,VicBarry,VicOffice
    
    BadScript.ps1
    Code:
    $OfficeSites = Import-Csv "C:\Scripts\OfficeSites.csv"
    $Userlist = Import-Csv "C:\Scripts\UserList.csv"
    
    
    ForEach ($User in $Userlist) {
    
    	ForEach ($OfficeList in $OfficeSites) {
    	If ($User.Office -eq $Officelist.Office) {Write-Host "$($User.Name) is in Site $($Officelist.Site)"}
    	}
    }
    
     
  10. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,507
    Location:
    Melbourne

    Thanks, thats kinda how I started, but then I thought it might not look at the list correctly

    I know what you mean with "but I'm a sysadmin, not a coder, so I don't know any better." I have a friend who does DevOps and we write completely different powershell scripts for the same task :)
     
  11. Dre_

    Dre_ Member

    Joined:
    May 25, 2014
    Messages:
    841
    Whats the best way of splatting this?

    Code:
    $TenantId = (Get-MsolPartnerContract -All | Select-Object -Property DefaultDomainName, Name, TenantId | Sort-Object -Property Name | Out-GridView -Title "Find the customer..." -PassThru).TenantId
    
    I want to avoid doing this, and how do I handle the PassThru?
    I'd rather go @params1 and have it include the Select-Object in there too.

    Code:
    $TenantId = (Get-MsolPartnerContract -All | Select-Object @params1 | Sort-Object @params2 | Out-GridView @params3 -PassThru).TenantId
    Am I even on the right path?
     
  12. Dale

    Dale Member

    Joined:
    Jul 28, 2002
    Messages:
    134
    Location:
    Melbourne
    As a non coder too I like using hash tables when you have a suitable foreign key.

    eg
    Code:
    $hash = Import-Csv c:\temp\OfficeSites.csv | Group-Object -AsHashTable -Property Office
    $hash.Get_Item("NSWOffice").site
    
    Cheers
    Dale
     
  13. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,507
    Location:
    Melbourne
    Thanks Dale, that might work. That task has been put on hold so I dont have to worry about it for now :)


    I have a new one for a friday afternoon

    Is there a way to append a variable with a number (to create a new variable) for each loop of a foreach loop ?
     
  14. Dale

    Dale Member

    Joined:
    Jul 28, 2002
    Messages:
    134
    Location:
    Melbourne
    Sometimes I find it handy when combining data from multiple sources to make a new object which the attributes I want.

    eg
    Code:
    $arrayOfObjects = @()
    
    $newObject = New-Object System.Object
    $newObject | Add-Member -MemberType NoteProperty -name "SourceOneAttribute" -value "Value"
    $newObject | Add-Member -MemberType NoteProperty -name "SourceTwoAttribute" -value "Value"
    
    $arrayOfObjects += $newObject
    
    Cheers
    Dale
     
  15. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,507
    Location:
    Melbourne
    Thats guys,

    I figured I best put a bit of context, that may help :)

    So Im building a form, part of it will have a list of OUs extracted from AD

    Code:
    $OUList = Get-ADOrganizationalUnit -Filter * -SearchBase "OUPath" | Select -ExpandProperty Name
    I then want to run the names through a loop to create each button with the name as the name of the OU


    Code:
    foreach ($OU in $OUList) {
    
        $Button1 = New-Object System.Windows.Forms.RadioButton
        $Button1.Location = '20,40'
        $Button1.size = '350,20'
        $Button1.Checked = $false 
        $Button1.Text = "$OU"
    
    
    SO on the second loop I want the button to be button2
     
  16. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,169
    Location:
    Brisbane
    If you don't like Dales solution above you can do something like this

    Code:
    for ($i = 1; $i -lt 99; $i++)
    { 
        New-Variable -Name "Variable$i" -Value $i -Force
    }
    
    Get-Variable -Name Variable*
    
     
  17. Embercide

    Embercide Member

    Joined:
    Jun 17, 2002
    Messages:
    1,818
    Location:
    Brisbane
    I didnt see something similar covered off here before, but what is a good way to subtract a file path (leaving just the filename) stored in a variable.

    I'm using this function to select a file
    Code:
    Function Get-FileName($initialDirectory)
    {
        [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
        
        $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
        $OpenFileDialog.initialDirectory = $initialDirectory
        #$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
        $OpenFileDialog.ShowDialog() | Out-Null
        $OpenFileDialog.filename
    }
    
    $UserSelection = Get-FileName
    Let's say $UserSelection = E:\temp\testfile.blah
    How can I then strip everything but the filename testfile.blah and store that as a seperate variable (eg $File) ?
     
  18. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,610
    You can Get-Item on $UserSelection and end up with a FileInfo object, that, among other things, has the property of Name

    Code:
    $MyFile = get-item $UserSelection
    $MyFile.Name
    
     
  19. Embercide

    Embercide Member

    Joined:
    Jun 17, 2002
    Messages:
    1,818
    Location:
    Brisbane
    This is too perfect :thumbup::thumbup:
    Also lets me do $MyFile.extension etc
    Cheers
     
  20. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,610
    It's nice, If I wasn't 1/2 a sleep, I'd probably have redone the function to return the object itself... but I am, so I wont
     

Share This Page

Advertisement: