Reputation: 147
I am trying to make a try statement with a foreach loop. Everything I look up shows this code should be right but for some reason it says
The try statement is missing its catch or Finally block
I have 0 idea why this is happening I made sure ALL { and } match up. Even if I take this out of the foreach loop, the try catches do not work.
Clear-Host
Clear-Content .\PS.txt
$args1 = Get-Content .\names.txt
$args1 = $args1.Split(",")
$ArrComputers = $args1
$i = 0
foreach ($Computer in $ArrComputers)
{
$Computer = $Computer.Trim(" ")
try{
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
"PASS " + $Computer +'|'
$HDDInfo = ""
$getIP, $computerSystem, $computerCPU, $hdds, $computerOS, $OS = " "
$i +=1
$z = 0
[string]$hdd = ""
$getIP =[string](Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet).IPAddress
$myOBJ = "" | Select-Object "computer"
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
$computerCPU = get-wmiobject Win32_Processor -Computer $Computer
$hdds = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
$OS = $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
"CPU: " + $computerCPU.Name
foreach ($computerhdd in $hdds) {
$z +=1
If($z -eq 1){
$PRHDDName = [string]$computerhdd.DeviceID
} else {
$SECHDDName = [string]$computerhdd.DeviceID
}
$HDDInfo = [string]($HDDInfo+"<"+$computerhdd.DeviceID+">" + "HDD Capacity/" + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB" )
"HDD Drive Letter: " + $HDDInfo
[string]$hdd = ([string]($computerHDD.Size/1GB) + [string]"," + [string]$hdd)
}
"RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
"OS: " + $OS
$temp = [string]$myOBJ.computer
[string]$myOBJ.computer = [string]("| !" + $Computer + ".! , +" + $getIP + '.+, a[' + $computerCPU.Name + ".] , ~" + $HDDInfo+ " ~, {" + $OS + ".} a|")
Add-Content .\PS.txt $myOBJ.computer
}
#End of try
catch[System.UnauthorizedAccessException]{
Add-Content .\PS.txt "| Failed |"
}catch{
Add-Content .\PS.txt "| Failed |"
"FAIL" + $_.Exception.GetType().FullName
}
}
Upvotes: 0
Views: 802
Reputation: 1999
there is a space missing between "catch" and "[System.UnauthorizedAccessException]"
Upvotes: 2