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.
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)