New To Iron Scripter

This topic contains 5 replies, has 4 voices, and was last updated by  Brandon Lundt 4 months ago.

  • Author
    Posts
  • #91547

    Robert Carlson
    Participant

    Hello,
    I'm new to IronScripter and though I'd try my hand at the prequel puzzle. I'm not sure to which faction my style most closely adheres. I've included my attempt at the first prequel puzzle below (Note: Normally i'd include help in my cmdlets but I haven't had time). Any advice/suggestions would be greatly appreciated.

    function Get-MonitorReport {
        
        [CmdletBinding()]
        
        param(
            
            [Parameter(Mandatory = $false)][string[]]$ComputerNames,
            [Parameter(Mandatory = $False)][System.Management.Automation.PSCredential]$Credential
        )
    
        BEGIN {
    
            $MonitorReport = @()
            $ComputerTypes = @{'0' = "Standalone Workstation"
                               '1' = "Member Workstation"
                               '2' = "Standalone Server"
                               '3' = "Member Server"
                               '4' = "Backup Domain Controller"
                               '5' = "Primary Domain Controller"}
        }
    
        
        PROCESS {
            
            if ($ComputerNames) {
    
                foreach ($ComputerName in $ComputerNames) {
    
                    if ($Credential) {
    
                        $Monitors = Get-CimInstance -Namespace root/wmi -Class wmiMonitorID -ComputerName $computerName -Credential $Credential
                        $ComputerSystem = Get-CimInstance -Class Win32_ComputerSystem -ComputerName $ComputerName -Credential $Credential
                        $ComputerBios = Get-CimInstance -Class Win32_BIOS -ComputerName $ComputerName -Credential $Credential
                    } else {
    
                        $Monitors = Get-CimInstance -Namespace root/wmi -Class wmiMonitorID -ComputerName $computerName
                        $ComputerSystem = Get-CimInstance -Class Win32_ComputerSystem -ComputerName $ComputerName
                        $ComputerBios = Get-CimInstance -Class Win32_BIOS -ComputerName $ComputerName
                    }
    
                    foreach ($Monitor in $Monitors) {
    
                        if ($monitor.SerialNumberID) {
    
                            $monitorSerialNumber = ($monitor.SerialNumberID | ForEach-Object { [char]$_ }) -join ""
                        } else {
    
                            $monitorSerialNumber = "n/a"
                        }
    
                        if ($monitor.UserFriendlyName) {
    
                            $monitorType = ($monitor.UserFriendlyName | ForEach-Object { [char]$_ }) -join ""
                        } else {
    
                            $monitorType = "n/a"
                        }
    
                        $monitorInformation = [pscustomobject]@{ComputerName=$ComputerSystem.Name
                                                                ComputerModel=$($ComputerSystem.Manufacturer + " " + $ComputerSystem.Model)
                                                                ComputerType=$ComputerTypes[$ComputerSystem.DomainRole.ToString()]
                                                                ComputerSerialNumber=$ComputerBios.SerialNumber
                                                                MonitorSerialNumber=$monitorSerialNumber
                                                                MonitorType=$monitorType}
                        $MonitorReport += $monitorInformation
                    }
                }
            } else {
    
                if ($Credential) {
    
                    $Monitors = Get-CimInstance -Namespace root/wmi -Class wmiMonitorID -Credential $Credential
                    $ComputerSystem = Get-CimInstance -Class Win32_ComputerSystem -Credential $Credential
                    $ComputerBios = Get-CimInstance -Class Win32_BIOS -Credential $Credential
                } else {
    
                    $Monitors = Get-CimInstance -Namespace root/wmi -Class wmiMonitorID
                    $ComputerSystem = Get-CimInstance -Class Win32_ComputerSystem
                    $ComputerBios = Get-CimInstance -Class Win32_BIOS
                }
    
                foreach ($Monitor in $Monitors) {
                    
                    if ($monitor.SerialNumberID) {
    
                        $monitorSerialNumber = ($monitor.SerialNumberID | ForEach-Object { [char]$_ }) -join ""
                    } else {
    
                        $monitorSerialNumber = "n/a"
                    }
                    
                    if ($monitor.UserFriendlyName) {
    
                        $monitorType = ($monitor.UserFriendlyName | ForEach-Object { [char]$_ }) -join ""
                    } else {
    
                        $monitorType = "n/a"
                    }
    
                    $monitorInformation = [pscustomobject]@{ComputerName=$ComputerSystem.Name
                                                            ComputerModel=$($ComputerSystem.Manufacturer + " " + $ComputerSystem.Model)
                                                            ComputerType=$ComputerTypes[$ComputerSystem.DomainRole.ToString()]
                                                            ComputerSerialNumber=$ComputerBios.SerialNumber
                                                            MonitorSerialNumber=$monitorSerialNumber
                                                            MonitorType=$monitorType}
                    
                    $MonitorReport += $monitorInformation
                }
            }
    
            Write-Output $MonitorReport
        }
    }
    
  • #91550

    Alex Aymonier
    Participant

    Tested on local laptop only: Works well with no computer name specified but when calling a computer name 'localhost' it errors out, look like line 36 needs to be fixed with the param name.

  • #91552

    Robert Carlson
    Participant

    Sorry, type. Thank you Alex

  • #91576

    Don Jones
    Keymaster

    As a note, we've never done Iron Scripter before, so everyone is new at it ;).

  • #91592

    Robert Carlson
    Participant

    Corrected version:

    function Get-MonitorReport {
        
        [CmdletBinding()]
        
        param(
            
            [Parameter(Mandatory = $true, ParameterSetName = "Remote")][string[]]$ComputerNames,
            [Parameter(Mandatory = $true, ParameterSetName = "Remote")][System.Management.Automation.PSCredential]$RemoteCredential,
            [Parameter(Mandatory = $false, ParameterSetName = "Remote")][switch]$Remote,
            [Parameter(Mandatory = $false, ParameterSetName = "Local")][switch]$Local
        )
    
        BEGIN {
    
            $MonitorReport = @()
            $ComputerTypes = @{'0' = "Standalone Workstation"
                               '1' = "Member Workstation"
                               '2' = "Standalone Server"
                               '3' = "Member Server"
                               '4' = "Backup Domain Controller"
                               '5' = "Primary Domain Controller"}
        }
    
        
        PROCESS {
            
            if ($Remote) {
                Write-Verbose ("Remote specified")
    
                foreach ($ComputerName in $ComputerNames) {
                    
                    try {
    
                        Write-Verbose $("Attempting to create CimSession to: " + $ComputerName)
                        $CimSession = @()
                        $CimSession = New-CimSession -ComputerName $ComputerName -Credential $RemoteCredential -ErrorAction Stop
                        Write-Verbose $CimSession
    
                    }  catch {
                
                        Write-Error $("Unable to create CimSession to: " + $ComputerName)
                    }
    
                    if ($CimSession) {
                     
                        $Monitors = Get-CimInstance -Namespace root/wmi -Class wmiMonitorID -CimSession $CimSession
                        $ComputerSystem = Get-CimInstance -Class Win32_ComputerSystem -CimSession $CimSession
                        $ComputerBios = Get-CimInstance -Class Win32_BIOS -CimSession $CimSession
    
                        Write-Verbose $("Removing: " + $CimSession)
                        Remove-CimSession -CimSession $CimSession
    
                        Write-Verbose $("Creating report object for: " + $ComputerName)
    
                        foreach ($Monitor in $Monitors) {
    
                            if ($monitor.SerialNumberID) {
    
                                $monitorSerialNumber = ($monitor.SerialNumberID | ForEach-Object { [char]$_ }) -join ""
            
                            } else {
    
                                $monitorSerialNumber = "n/a"
                            }
    
                            if ($monitor.UserFriendlyName) {
    
                                $monitorType = ($monitor.UserFriendlyName | ForEach-Object { [char]$_ }) -join ""
            
                            } else {
    
                                $monitorType = "n/a"
                            }
    
                            $monitorInformation = [pscustomobject]@{ComputerName=$ComputerSystem.Name
                                                                    ComputerModel=$($ComputerSystem.Manufacturer + " " + $ComputerSystem.Model)
                                                                    ComputerType=$ComputerTypes[$ComputerSystem.DomainRole.ToString()]
                                                                    ComputerSerialNumber=$ComputerBios.SerialNumber
                                                                    MonitorSerialNumber=$monitorSerialNumber
                                                                    MonitorType=$monitorType}
                            $MonitorReport += $monitorInformation
                        }
                    }
                }
            }
            elseif ($Local) {
    
                if ($ComputerNames) {
            
                    throw "If you want to specify target computers, please add the -Remote switch"
                }
                
                Write-Verbose "Querying local machine"
                $Monitors = Get-CimInstance -Namespace root/wmi -Class wmiMonitorID
                $ComputerSystem = Get-CimInstance -Class Win32_ComputerSystem
                $ComputerBios = Get-CimInstance -Class Win32_BIOS
                
                Write-Verbose "Creating report object for local machine"
    
                foreach ($Monitor in $Monitors) {
                    
                    if ($monitor.SerialNumberID) {
    
                        $monitorSerialNumber = ($monitor.SerialNumberID | ForEach-Object { [char]$_ }) -join ""
                
                    } else {
    
                        $monitorSerialNumber = "n/a"
                
                    }
                    
                    if ($monitor.UserFriendlyName) {
    
                        $monitorType = ($monitor.UserFriendlyName | ForEach-Object { [char]$_ }) -join ""
                
                    } else {
    
                        $monitorType = "n/a"
                
                    }
    
                    $monitorInformation = [pscustomobject]@{ComputerName=$ComputerSystem.Name
                                                            ComputerModel=$($ComputerSystem.Manufacturer + " " + $ComputerSystem.Model)
                                                            ComputerType=$ComputerTypes[$ComputerSystem.DomainRole.ToString()]
                                                            ComputerSerialNumber=$ComputerBios.SerialNumber
                                                            MonitorSerialNumber=$monitorSerialNumber
                                                            MonitorType=$monitorType}
                    
                    $MonitorReport += $monitorInformation
                
                }
            } else {
    
                throw "Please specify either -Local or -Remote"
            }
    
            Write-Output $MonitorReport
        
        }
    }
    
    • #91600

      Brandon Lundt
      Participant

      Nice! I like what you did with lines 16 to 22. If it helps, lines 88 to 91 are redundant and will never evaluate to true because you implemented ParameterSetNames. I had attempted to execute your function with both -Local and -ComputerNames and got this:

      Get-MonitorReport : Parameter set cannot be resolved using the specified named parameters.
      At line:142 char:1
      + Get-MonitorReport -ComputerNames "srv01" -Local
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidArgument: (:) [Get-MonitorReport], ParameterBindingException
          + FullyQualifiedErrorId : AmbiguousParameterSet,Get-MonitorReport
      

You must be logged in to reply to this topic.