Saya sudah mencoba melampirkan yang berikut ini dalam pernyataan if sehingga saya dapat menjalankan perintah lain jika ini berhasil:
Get-WmiObject -Class Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'" | Foreach-Object {
$Localdrives += $_.Path
tetapi saya tidak tahu bagaimana melakukannya. Saya bahkan mencoba membuat fungsi tetapi saya juga tidak tahu bagaimana cara memeriksa apakah fungsi tersebut berhasil diselesaikan.
Coba variabel otomatis $?
$share = Get-WmiObject -Class Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'"
if($?)
{
"command succeeded"
$share | Foreach-Object {...}
}
else
{
"command failed"
}
Dari tentang_Variabel_Otomatis
:
$?
Contains the execution status of the last operation. It contains
TRUE if the last operation succeeded and FALSE if it failed.
...
$LastExitCode
Contains the exit code of the last Windows-based program that was run.
Anda bisa mencoba:
$res = get-WmiObject -Class Win32_Share -Filter "Description='Default share'"
if ($res -ne $null)
{
foreach ($drv in $res)
{
$Localdrives += $drv.Path
}
}
else
{
# your error
}