• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Stupid powershell script

narzy

Elite Member
I'm having a bit of trouble with my powershell script

I can't seem to get option 5 of the script working, I also need a little help with option 4

Option 4 is suppose to ask the user an IP address to ping and then create a popup with the address that the ping was sent to and the hostname of the computer that responds.

I have it so that it will ping a range of addresses and respond with the address and hostname of computers that respond to the ping but can't figure out how to get the popup working and only ping and output the hostname of the computer the responds

Option 5 throws an error when I try to run it. It works just fine if I run it as a stand alone script...

This is for a mid term so if you just want to give some pointers that would be great.

Code:
$wshshell = new-object -ComObject wscript.shell
$wshnetwork = New-Object -ComObject wscript.network

#Build menu here
$strMenu = "Mid-Term Menu Tim Thorpe `n`n" 
$strMenu = $strMenu + "0. Quit this Program `n"
$StrMenu = $strMenu + "1. Hello Logged-On User `n"
$strMenu = $strMenu + "2. Computer Information `n"
$strMenu = $strMenu + "3. List Mapped Drives `n"
$strMenu = $strMenu + "4. Ping a Computer `n"
$strMenu = $strMenu + "5. Computer's Domain Role `n"
#add the rest of menu items



#Create Title bar
$strTitle = "Menu for Mid-Term"

#Create Menu Options
#Text between quotes goes on the button & indicates that it is a button
$0 = New-Object System.Management.Automation.Host.ChoiceDescription " &0 "
$1 = New-Object System.Management.Automation.Host.ChoiceDescription " &1 "
$2 = New-Object System.Management.Automation.Host.ChoiceDescription " &2 "
$3 = New-Object System.Management.Automation.Host.ChoiceDescription " &3 "
$4 = New-Object System.Management.Automation.Host.ChoiceDescription " &4 "
$5 = New-Object System.Management.Automation.Host.ChoiceDescription " &5 "
#Create the rest of the buttons...

#Create the menu -- Add additional menu options between the ()
$objOptions = [System.Management.Automation.Host.ChoiceDescription[]] ($0,$1,$2,$3,$4,$5)

# Do this until the user quits by pressing the quit button
$intQuit = 1
do {
    $result = $HOST.ui.PromptForChoice($strTitle,$strMenu,$objOptions,0)
    #use switch to do tasks based on the selected option
    switch($result)
        {
          #Menu option 0, quit
          0 {
             $intQuit = 0
             $wshShell.popup("Quit! This is really the only thing that works") 
            }  
          
################################
#Menu option 1 - Hello world
          1 {
          $strUsername = "Hello " + $wshNetwork.Username + "!"
          $wshShell.popup($strUsername)
            #end of option 1
            }
         
################################
#Menu option 2 - Computer information
          2 {
          #get computer information and store it in a var
            $cpu = Get-WmiObject win32_processor

          #create a switch to output the comp info
            switch($cpu.architecture)
            {
                0{$arch = "This is an x86 processor"}
                1{$arch = "This is a MIPS processor"}
                2{$arch = "This is an Alpha processor"}
                3{$arch = "This is a PowerPC processor"}
                6{$arch = "This is an IPF processor"}
                9{$arch = "This is an x64 processor"}
                default{$arch = "Unknown processor type"}
            }

        #store the clock speed and cache in a ver
        #get the clock speed
        $clockspeed = "The clock speed of the processor is: " + $cpu.currentClockSpeed + " Mhz"
        #get the size of the L2 cache
        $L2CacheSize = "The L2 Cache size is: " + $cpu.L2cacheSize + " Kb"

        $wshshell.popup("
               $arch
               $Clockspeed 
               $l2cacheSize 
               ", 0, "Computer Information", 64)
         
#end of option 2
            } 
          
################################
          #Option 3 - List mapped network drives
          3 {
                #store the currently attached network drive in a var
                $coldrives = $wshnetwork.enumnetworkdrives()

                #display a popup that lists the currently available network drives
                $wshshell.Popup("Current Available Network Drives `n" + $coldrives, 0, "Available Network Drives",64)

                #display the currently available network drives in the shell
                write-host $coldrives
          
         #end of option 3
            }
         
################################
         #Option 4 - Ping a computer and return computer name
         4  {
         
#Objective ping all of hosts in a range of IP addresses and only show the computers that respond to the ping with their
#hostname
###########
#Store the number of pings in an int var
[int]$intPing = 254

#store the network to be pingged in a string var

[string]$strNetwork = "192.168.1."
#use 10.145.146. as intNetwork

#create a for loop to ping each computer
for ($i=1;$i -le $intPing;$i++)
    {
    #send the ping command
    $strQuery = "select * from win32_pingstatus where address = '"+$strnetwork + $i +"'"
    $wmi = get-wmiobject -query $strquery

#    write-host "Pinging $strNetwork$i ..."
        #use an if statement to get the hostname of each computer that responds to ping
        if ($wmi.statuscode -eq 0)
        {
        $strIP = $strNetwork+$i
        $strHostname = [System.net.dns]::GetHostByAddress($strIP).hostname
        #output the network address that responds to the ping and the hostname of the computer
        Write-host $StrIp $strHostname
        }
    }         
         #end of option 4
            }
################################            
         #Option 5 - Computers domain role
         5  {
         
#store the domain role in a var
$domainObject = Get-WmiObject win32_ComputerSystem

$comprole = "Computer " + $DomainObject.Name + " is "

#use if else if statements to determine computer role and store that role in a var
if ($DomainObject.domainRole -eq 0)
    {
    $comprole = $comprole + "a Stand-Alone Workstation"
    }

elseif ($DomainObject.domainrole -eq 1)
    {
    $comprole = $comprole + "a Member Workstation"
    }

elseif ($DomainObject.DomainRole -eq 2)
    {
    $comprole = $comprole + "a Stand-alone Server"
    }
    
elseif ($DomainObject.DomainRole -eq 3)
    {
    $comprole = $comprole + "a Member Server"
    }

elseif ($DomainObject.DomainRole -eq 4)
    {
    $comprole = $comprole + "a Backup Domain Controller"
    }

elseif ($DomainObject.DomainRole -eq 5)
    {
    $comprole = $comprole + "a Primary Domain Controller"
    }

#if none are true then tell the user that the computer role could not be determined

else
    {
    $comprole = "Your computers role could not be determined"
    }

#display the information
$wshell.popup("
               $arch
               $Clockspeed 
               $l2cacheSize 
               $CompRole", 0, "Computer Information", 64)


         #end of option 5
            }

################################
        #end of switch         
        }
        


    #end of do
    } until ($intQuit -eq 0)
 
You cannot call a method on a null-valued expression.
At C:\Users\Tim\Documents\Powershell\midterm ping.ps1:181 char:14
+ $wshell.popup <<<< ("$CompRole", 0, "Computer Information", 64)
+ CategoryInfo : InvalidOperation: (popup:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
 
Last edited:
OMFG PEBKAC ERROR!!!


line 1 - $wshshell = new-object -ComObject wscript.shell
line 190 - $wshell.popup

😉
 
Oh and thanks for posting this question. I've been wanting to learn more about powershell after seeing it's potential for automating stuff.
 
My final working script! YAY!

Code:
##################
#Tim Thorpe
#2/21/2010
#MidTerm
#Create a popup box with a number of options to preform a number of tasks to display information about that current computer
###################

$wshshell = new-object -ComObject wscript.shell
$wshnetwork = New-Object -ComObject wscript.network

#Build menu here
$strMenu = "Mid-Term Menu Tim Thorpe `n`n" 
$strMenu = $strMenu + "0. Quit this Program `n"
$StrMenu = $strMenu + "1. Hello Logged-On User `n"
$strMenu = $strMenu + "2. Computer Information `n"
$strMenu = $strMenu + "3. List Mapped Drives `n"
$strMenu = $strMenu + "4. Ping a Computer `n"
$strMenu = $strMenu + "5. Computer's Domain Role `n"
$strMenu = $strMenu + "6. Computer's Architecture `n"

#add the rest of menu items



#Create Title bar
$strTitle = "Menu for Mid-Term"

#Create Menu Options
#Text between quotes goes on the button & indicates that it is a button
$0 = New-Object System.Management.Automation.Host.ChoiceDescription " &0 "
$1 = New-Object System.Management.Automation.Host.ChoiceDescription " &1 "
$2 = New-Object System.Management.Automation.Host.ChoiceDescription " &2 "
$3 = New-Object System.Management.Automation.Host.ChoiceDescription " &3 "
$4 = New-Object System.Management.Automation.Host.ChoiceDescription " &4 "
$5 = New-Object System.Management.Automation.Host.ChoiceDescription " &5 "
$6 = New-Object System.Management.Automation.Host.ChoiceDescription " &6 "

#Create the rest of the buttons...

#Create the menu -- Add additional menu options between the ()
$objOptions = [System.Management.Automation.Host.ChoiceDescription[]] ($0,$1,$2,$3,$4,$5,$6)

# Do this until the user quits by pressing the quit button
$intQuit = 1
do {
    $result = $HOST.ui.PromptForChoice($strTitle,$strMenu,$objOptions,0)
    #use switch to do tasks based on the selected option
    switch($result)
        {
          #Menu option 0, quit
          0 {
             $intQuit = 0
             $wshShell.popup("Quit! This is really the only thing that works") 
            }  
          
################################
#Menu option 1 - Hello world
          1 {
          $strUsername = "Hello " + $wshNetwork.Username + "!"
          $wshShell.popup($strUsername)
            #end of option 1
            }
         
################################
#Menu option 2 - Computer information - Username, Domain, Computer Name
          2 {
         #create carrige return between item output crlf = carriage return line format
$crlf = [environment]::newline

#create popup var
$strpopup = "Current Username: " + $wshnetwork.UserName +  $crlf

#add the computer name to the strpopup var
$strpopup = $strpopup + "Computer Name: " + $wshnetwork.ComputerName + $crlf

#add the domain name to the strpopup var
$strpopup = $strpopup + "Domain Name: " + $wshnetwork.UserDomain

#show a popup with all of the information stored in strpopup var that lasts for 7 seconds and has an exclimation point icon
$wshShell.Popup($strpopup, 7, "Computer Information",64)
         
#end of option 2
            } 
          
################################
          #Option 3 - List mapped network drives
          3 {
                #store the currently attached network drive in a var
                $coldrives = $wshnetwork.enumnetworkdrives()

                #display a popup that lists the currently available network drives
                $wshshell.Popup("Current Available Network Drives `n" + $coldrives, 0, "Available Network Drives",64)

                #display the currently available network drives in the shell
                write-host $coldrives
          
         #end of option 3
            }
         
################################
         #Option 4 - Ping a computer and return computer name
         4  {
         
#Objective ping all of hosts in a range of IP addresses and only show the computers that respond to the ping with their
#hostname
###########
#ask the user for an IP address to ping
$IPping = read-host

#Ping the computer and get the results of the ping
$strToPing = "select * from win32_pingstatus where address = '"+$IPping +"'"
$wmi = Get-WmiObject -Query $strToPing
    #Determine the hostname of the computer
        if ($wmi.statuscode -eq 0)
        {
        $strIP = $IPping
        $strHostname = [System.net.dns]::GetHostByAddress($strIP).hostname
        #output the network address that responds to the ping and the hostname of the computer
        $PingInfo = "IP address: " + $StrIp +"'s Hostname is: "+ $strHostname
        }
#pop up the IP address that was pinged and the hostname of the device that responds
$wshShell.popup($PingInfo)
             
         #end of option 4
            }
################################            
         #Option 5 - Computers domain role
         5  {
         
#store the domain role in a var
$domainObject = Get-WmiObject win32_ComputerSystem

$comprole = "Computer " + $DomainObject.Name + " is "

#use if else if statements to determine computer role and store that role in a var
if ($DomainObject.domainRole -eq 0)
    {
    $comprole = $comprole + "a Stand-Alone Workstation"
    }

elseif ($DomainObject.domainrole -eq 1)
    {
    $comprole = $comprole + "a Member Workstation"
    }

elseif ($DomainObject.DomainRole -eq 2)
    {
    $comprole = $comprole + "a Stand-alone Server"
    }
    
elseif ($DomainObject.DomainRole -eq 3)
    {
    $comprole = $comprole + "a Member Server"
    }

elseif ($DomainObject.DomainRole -eq 4)
    {
    $comprole = $comprole + "a Backup Domain Controller"
    }

elseif ($DomainObject.DomainRole -eq 5)
    {
    $comprole = $comprole + "a Primary Domain Controller"
    }

#if none are true then tell the user that the computer role could not be determined

else
    {
    $comprole = "Your computers role could not be determined"
    }

#display the information
$wshShell.popup("$CompRole", 0, "Computer Information", 64)


         #end of option 5
            }

###############################
#Option 6 - Computer Architecture
    6 {
 #get computer information and store it in a var
            $cpu = Get-WmiObject win32_processor

          #create a switch to output the comp info
            switch($cpu.architecture)
            {
                0{$arch = "This is an x86 processor"}
                1{$arch = "This is a MIPS processor"}
                2{$arch = "This is an Alpha processor"}
                3{$arch = "This is a PowerPC processor"}
                6{$arch = "This is an IPF processor"}
                9{$arch = "This is an x64 processor"}
                default{$arch = "Unknown processor type"}
            }

        #store the clock speed and cache in a ver
        #get the clock speed
        $clockspeed = "The clock speed of the processor is: " + $cpu.currentClockSpeed + " Mhz"
        #get the size of the L2 cache
        $L2CacheSize = "The L2 Cache size is: " + $cpu.L2cacheSize + " Kb"

        $wshshell.popup("
               $arch
               $Clockspeed 
               $l2cacheSize 
               ", 0, "Computer Information", 64)
        }
################################
        #end of switch         
        }
        


    #end of do
    } until ($intQuit -eq 0)
 
What's funny was I initially thought it was one of the variables being passed into the popup method that was null.

So I started with checking if $comprole was null and have it exit out of the script, but it never was null and I was still getting the error 😀
 
Back
Top