0
我想弄清楚如何编写powershell脚本,如果PowerShell无法连接到\ 10.10.10.10 \ C $ \ Users,它应该尝试连接到\ 10.10.10.10 \ C $ \文件和设置IF Get-ChildItem:无法找到路径然后连接到其他路径
到目前为止,我有以下的代码
$Win32OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName '10.10.10.10'
$SysDrv = $Win32OS.SystemDrive
$SysDrv = $SysDrv.Replace(":","$")
$ProfDrv = "\\" + '10.10.10.10' + "\" + $SysDrv
$ProfLoc = Join-Path -Path $ProfDrv -ChildPath "User"
try
{
Get-ChildItem $ProfLoc | ? { $_.PsIsContainer} | Sort LastWriteTime -Descending | Select -expand Name -First 1
}
catch [Exception]
{
return "CannotFindLastUser"
}
如果路径\ 10.10.10.10 \ C $ \用户不存在,它给出了一个错误
Get-ChildItem : Cannot find path '\\10.10.10.10\C$\Users' because it does not exist.
如何瓒GE代码,使得其尝试连接到\ 10.10.10.10 \ C $ \文件和设置,否则它抛出一个用户友好的消息
EDIT
这里是整个脚本,其中参数是形式“发送器IP = 10.10.10.10'
$line_array = @()
$multi_array = @()
[hashtable]$my_hash = @{}
$Sender_IP = $NULL
$Win32OS = $NULL
$Build = $NULL
$folder = $NULL
$SysDrv = $NULL
foreach ($i in $args){
$line_array+= $i.split(" ")
}
foreach ($j in $line_array){
$multi_array += ,@($j.split("="))
}
foreach ($k in $multi_array){
$my_hash.add($k[0],$k[1])
}
$Sender_IP = $my_hash.Get_Item("sender-ip")
try{
Test-Connection $Sender_IP -count 1 -ErrorAction Stop | out-null
}
catch [Exception]
{
$userId = "userId=CannotPing"
return $userId
}
try{
$OS = (Get-WmiObject Win32_OperatingSystem -ComputerName $Sender_IP).Name
}
catch [Exception]{
$userId = "userId=CannotConnectToWMI"
return $userId
}
$Win32OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Sender_IP
$SysDrv = $Win32OS.SystemDrive
$SysDrv = $SysDrv.Replace(":","$")
$ProfDrv = "\\" + $Sender_IP + "\" + $SysDrv
$ProfLoc = Join-Path -Path $ProfDrv -ChildPath "Users"
try{
$userId = Get-ChildItem $ProfLoc | ? { $_.PsIsContainer} | Sort LastWriteTime -Descending | Select -expand Name -First 1
$userId = "userId="+$userId
return $userId
}catch [Exception]
{
$userId = "userId=CannotFindLastUser"
return $userId
}
我的脚本ping服务器,尝试连接到WMI,如果这两个步骤成功,那么它会尝试连接到路径\\ 10.10.10.10 \ C $ \ Users – Glowie
OK,然后使用Test-路径“来确保它存在。 – alroc