PS: Creating Scripts

3 minute read

Description:

This is a general idea of how to go about creating scripts in Powershell.

To Resolve:

  1. Start with a template script.

  2. Almost all functions will need to have a parameter so the user can specify for their environment. You can let it allow multiple by adding “[]” in the type declaration

    1
    
    [string[]]$ComputerName
    
  3. Then, in the process block, just loop through each that the user passed. If they only passed one that is fine:

    1
    2
    3
    4
    
    ForEach ($Computer in $ComputerName)
    {
    # Do something for each individual computer
    }
    
  4. The first steps will be fine enough on their own, but sometimes you want to put each iteration into a table and then return that at the end:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    
    Foreach ($Computer in $ComputerName)
                {
    
                   If (!([String]::IsNullOrWhiteSpace($Computer)))
                   {
    
                      If (Test-Connection -Quiet -Count 1 -Computer $Computer)
                      {
    
                            $Progress = @{}
                            $Progress.Activity = "Getting Sytem Information..." 
                            $Progress.Status = ("Percent Complete:" + "{0:N0}" -f ((($i++) / $ComputerName.count) * 100) + "%")
                            $Progress.CurrentOperation = "Processing $($Computer)..."
                            $Progress.PercentComplete = ((($j++) / $ComputerName.count) * 100)
                            Write-Progress @Progress
                         
                         
                            $CimSession = Get-LHSCimSession -ComputerName $Computer -Credential $Credential
    
                            $computerSystem = Get-CimInstance CIM_ComputerSystem -CimSession $CimSession
                            $computerBIOS = Get-CimInstance CIM_BIOSElement -CimSession $CimSession
                            $computerOS = Get-CimInstance CIM_OperatingSystem -CimSession $CimSession
                            $computerCPU = Get-CimInstance CIM_Processor -CimSession $CimSession
                            $computerHDD = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID = 'C:'" -CimSession $CimSession
    
                            $ComputerObject = [Ordered]@{}
                            $ComputerObject.ComputerName = $computerSystem.Name
                            $ComputerObject.LastReboot = $computerOS.LastBootUpTime
                            $ComputerObject.OperatingSystem = $computerOS.OSArchitecture + " " + $computerOS.caption
                            $ComputerObject.Model = $computerSystem.Model
                            $ComputerObject.RAM = "{0:N2}" -f [int]($computerSystem.TotalPhysicalMemory / 1GB) + "GB"
                            $ComputerObject.DiskCapacity = "{0:N2}" -f ($computerHDD.Size / 1GB) + "GB"
                            $ComputerObject.TotalDiskSpace = "{0:P2}" -f ($computerHDD.FreeSpace / $computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace / 1GB) + "GB)"
                            $ComputerObject.CurrentUser = $computerSystem.UserName
                               
                            Write-Output "ComputerName: $($ComputerObject.ComputerName.ToString())"
                            Write-Output "LastReboot: $($ComputerObject.LastReboot.ToString())"
                            Write-Output "OperatingSystem: $($ComputerObject.OperatingSystem.ToString())"
                            Write-Output "Ram: $($ComputerObject.RAM.ToString())"
                            Write-Output "TotalDiskSpace: $($ComputerObject.TotalDiskSpace.ToString())"
                            Write-Output "CurrentUser: $($ComputerObject.CurrentUser.ToString())"
                            Write-Output "####################<Break>####################"
    
                            $ComputerObjects += $ComputerObject
                               
                            Remove-CimSession -CimSession $CimSession 
    
                      }
    
                      Else
                      {
                            Write-Output "Remote computer was not online."
                            $ComputerObject = [Ordered]@{}
                            $ComputerObject.ComputerName = $computer
                            $ComputerObject.LastReboot = "Unable to ping. Make sure the computer is turned on and ICMP inbound ports are opened."
                            $ComputerObject.OperatingSystem = "$null"
                            $ComputerObject.Model = "$null"
                            $ComputerObject.RAM = "$null"
                            $ComputerObject.DiskCapacity = "$null"
                            $ComputerObject.TotalDiskSpace = "$null"
                            $ComputerObject.CurrentUser = "$null"
    
                            $ComputerObjects += $ComputerObject                     
                      }
                   }
    
                   Else
                   {
                      Write-Output "Computer name was not in a usable format"
                      $ComputerObject.ComputerName = "Value is null. Make sure computer name is not blank"
                      $ComputerObject.LastReboot = "$Null"
                      $ComputerObject.OperatingSystem = "$null"
                      $ComputerObject.Model = "$null"
                      $ComputerObject.RAM = "$null"
                      $ComputerObject.DiskCapacity = "$null"
                      $ComputerObject.TotalDiskSpace = "$null"
                      $ComputerObject.CurrentUser = "$null"
    
                      $ComputerObjects += $ComputerObject   
                   }
                }
    
  5. Ignoring the Cim-Session part, what this is doing is looping through one more computers and logging their results into a log file. The first IF block just makes sure you didn’t enter a blank computer name, the second sees if the computer is online, and finally => it processes each computer in the list by connecting to them and putting the results into an object called “$ComputerObject.

  6. But we don’t want to stop there because there can be many computers and we want a list at the end. So what we do is place each $ComputerObject into a $ComputerObjects array. So now, we go back to the Begin block and initialize the array:

    1
    
    $ComputerObjects = @()
    
  7. Now in the End block, you can just display the results:

    1
    
    $ComputerObjects
    
  8. This works fine for displaying on a screen, but not to a text file. So we do one last thing instead:

    1
    2
    
    $a = $ComputerObjects | Out-String
    Write-Output $a
    
  9. This is a way to convert an object to a string that way the log will look like what is displayed on the screen.

Comments