Ответ 1
вы можете попробовать:
$res = get-WmiObject -Class Win32_Share -Filter "Description='Default share'"
if ($res -ne $null)
{
foreach ($drv in $res)
{
$Localdrives += $drv.Path
}
}
else
{
# your error
}
Я попытался включить следующее в оператор if, чтобы я мог выполнить другую команду, если это будет выполнено:
Get-WmiObject -Class Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'" | Foreach-Object {
$Localdrives += $_.Path
но я не могу понять, как это сделать. Я даже попытался создать функцию, но я не мог понять, как проверить, успешно ли выполнена эта функция.
вы можете попробовать:
$res = get-WmiObject -Class Win32_Share -Filter "Description='Default share'"
if ($res -ne $null)
{
foreach ($drv in $res)
{
$Localdrives += $drv.Path
}
}
else
{
# your error
}
Попробуйте $? автоматическая переменная:
$share = Get-WmiObject -Class Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'"
if($?)
{
"command succeeded"
$share | Foreach-Object {...}
}
else
{
"command failed"
}
$?
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.