错误:语法无效。默认选项不允许超过‘2’次

错误:语法无效。默认选项不允许超过‘2’次

我正在尝试运行下面的 powershell 脚本来识别连接到计算机的 USB 或 SCSI 接口类型,如果我不使用IFand-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

错误:语法无效。默认选项不允许使用超过“2”次。C:\temp\AddOSDUSBDrive_NEW.ps1:10 : 1 + setx OSDUSBDrive $letters /M + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (错误:语法...超过 2 次:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError 输入“SETX /?”以显示语法。

任何想法?

答案1

使用setx OSDUSBDrive "$letters" /M

应用SETX.exe句法图案

SETX [/s Computer [Credentials]] Variable Value [/m]
                                          ↑↑↑↑↑

$letters如果包含更多内容,则可能是错误的字母例如F:,,G:H:

 setx OSDUSBDrive $letters /M
 #                ↑↑↑↑↑↑↑↑        this evaluates to
 setx OSDUSBDrive F: G: H: /M
 #                ↑↑ ↑↑ ↑↑        three space-separated values (WRONG)

已调整(见语法:转义符、分隔符和引号):

 setx OSDUSBDrive "$letters" /M
 #                ↑↑↑↑↑↑↑↑↑↑       this evaluates to
 setx OSDUSBDrive "F: G: H:" /M
 #                 ↑↑↑↑↑↑↑↑        one string value (quotes = escape characters)

而且,我将对第一个进行IF如下调整:

if ( $diskdrive = ( Get-WmiObject win32_diskdrive | 
        Where-Object { $_.interfacetype -in ('USB','SCSI') } ) ) {

解释:-or运算符表示逻辑或:TRUE即,如果第一个操作数的计算结果为TRUE,则其他操作数永远不会被计算。

例如,运行

Remove-Variable a, b -ErrorAction SilentlyContinue
if ( ($a = 1) -or ($b = 3) ) { Write-host "a=$a b=$b" }

无法检索变量“$b”,因为尚未设置。

if ( ($a = $null) -or ($b = 3) ) { Write-host "a=$a b=$b" }

a=b=3

答案2

我实际上按照你的建议添加了这个:

如果($diskdrive =(Get-WmiObject win32_diskdrive | Where-Object {$_.interfacetype -in('USB','SCSI')})){

当我手动运行该脚本时,它运行正常,但是当从 SCCM 调用它时,我收到以下错误

您必须在“-”运算符的右侧提供一个值表达式。TaskSequencePSHost 3/6/2018 4:36:37 PM 0 (0x0000) 在 D:\SMS\PKG\SE1001B5\AddOSDUSBDrive_NEW.ps1:2 char:42 + Where-Object { $_.interfacetype - <<<< in ('USB','SCSI') } ) ){ TaskSequencePSHost 3/6/2018 4:36:37 PM 0 (0x0000) ParserError: (:) [], ParseException TaskSequencePSHost 3/6/2018 4:36:37 PM 0 (0x0000) TSHOST: 脚本已完成,返回代码为 0 TaskSequencePSHost 3/6/2018 4:36:37 PM 0 (0x0000)

相关内容