.png)
Estou tentando executar o script do PowerShell abaixo para identificar o tipo de interface USB ou SCSI conectada ao computador, o script funciona bem se eu não usar os operadores IF
e-Or
if(($diskdrive = (gwmi win32_diskdrive | ?{$_.interfacetype -eq 'USB'})) -Or ($diskdrive =(gwmi win32_diskdrive | ?{$_.interfacetype -eq 'SCSI'}))){
$letters = $diskdrive | %{gwmi -Query "ASSOCIATORS OF
{Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE
AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS
OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass =
Win32_LogicalDiskToPartition"} | %{$_.Deviceid}
setx OSDUSBDrive $letters /M
Write-Output $diskdrive, $letters}
Start-Sleep -s 5
if (-not ("Win32.NativeMethods" -as [Type]))
{
# import sendmessagetimeout from win32
Add-Type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
}
$HWND_BROADCAST = [IntPtr] 0xffff;
$WM_SETTINGCHANGE = 0x1a;
$result = [UIntPtr]::Zero
# notify all windows of environment block change
[Win32.Nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [UIntPtr]::Zero, "Environment", 2, 5000, [ref] $result);
Start-Sleep -s 5
ERRO: Sintaxe inválida. A opção padrão não é permitida mais de '2' vez(es). C:\temp\AddOSDUSBDrive_NEW.ps1:10 : 1 + setx OSDUSBDrive $letras /M + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (Erro: Sintaxe...mais 2 vezes.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Entrez "SETX /?" para exibir a sintaxe.
Qualquer ideia?
Responder1
Usarsetx OSDUSBDrive "$letters" /M
AplicadoSETX.exe
sintaxepadrão
SETX [/s Computer [Credentials]] Variable Value [/m] ↑↑↑↑↑
pode estar errado se $letters
contiver maiscartaspor exemplo F:
, G:
,H:
setx OSDUSBDrive $letters /M
# ↑↑↑↑↑↑↑↑ this evaluates to
setx OSDUSBDrive F: G: H: /M
# ↑↑ ↑↑ ↑↑ three space-separated values (WRONG)
Ajustado (verSintaxe: caracteres de escape, delimitadores e aspas):
setx OSDUSBDrive "$letters" /M
# ↑↑↑↑↑↑↑↑↑↑ this evaluates to
setx OSDUSBDrive "F: G: H:" /M
# ↑↑↑↑↑↑↑↑ one string value (quotes = escape characters)
Além disso, eu ajustaria o primeiro IF
da seguinte maneira:
if ( $diskdrive = ( Get-WmiObject win32_diskdrive |
Where-Object { $_.interfacetype -in ('USB','SCSI') } ) ) {
Explicação: -or
operador significa OR lógico:TRUE
quando querou seja, se o primeiro operando for avaliado como TRUE
, então os outros operandos nunca serão avaliados.
Por exemplo, execute
Remove-Variable a, b -ErrorAction SilentlyContinue
if ( ($a = 1) -or ($b = 3) ) { Write-host "a=$a b=$b" }
A variável '$b' não pode ser recuperada porque não foi definida.
if ( ($a = $null) -or ($b = 3) ) { Write-host "a=$a b=$b" }
uma = b = 3
Responder2
Na verdade, adicionei isso como você sugeriu:
if ($diskdrive = (Get-WmiObject win32_diskdrive | Where-Object { $_.interfacetype -in ('USB','SCSI') } ) ) {
o script funciona bem quando o executo manualmente, mas recebo o erro abaixo quando é chamado pelo SCCM
Você deve fornecer uma expressão de valor no lado direito do operador '-'. TaskSequencePSHost 06/03/2018 16:36:37 PM 0 (0x0000) Em D:\SMS\PKG\SE1001B5\AddOSDUSBDrive_NEW.ps1:2 char:42 + Where-Object { $_.interfacetype - <<<< in ( 'USB','SCSI') } ) ){ TaskSequencePSHost 06/03/2018 16:36:37 0 (0x0000) ParserError: (:) [], ParseException TaskSequencePSHost 06/03/2018 16:36:37 0 (0x0000) TSHOST: Script concluído com código de retorno 0 TaskSequencePSHost 06/03/2018 16:36:37 0 (0x0000)