'get-ciminstance win32_userprofile -CimSession | select' WITHIN DO & SWITCH 语句不起作用

问题描述

下面的 Cmdlet 工作正常,但在底部代码块的 do & switch 语句中没有任何作用?在 ISE 中调试不提供任何帮助。删除 | Select-Object 确实使它起作用,但会产生太多信息。删除 -Cimsession $hostname 确实使它起作用。所以问题似乎与远程 PC 和/或 SELECT 语句有关。

Get-CimInstance Win32_UserProfile -Cimsession $hostname | Select-Object -Property LocalPath,LastUseTime

function Show-Menu {
    Write-Host "
    1)Option A
    2)Option B
    3)User Profiles of Remote PC
    "}
DO {Show-Menu
    $UserChoice = Read-Host "Enter # of tool you want to run"
    $hostname=Read-Host "enter hostname"
    switch ($UserChoice) {
        1 {'You choose opt1'}
        2 {'You choose opt2'}
        3 {Get-CimInstance Win32_UserProfile -Cimsession $hostname | Select-Object -Property LocalPath,LastUseTime}
   }
} UNTIL ($hostname -eq '')
  • 此 cmdlet 存在相同问题:{Get-WMIObject Win32_UserProfile -ComputerName $hostname | Select-Object -Property LocalPath,LastUseTime}
  • 有效,但间隔有趣:{Get-WMIObject Win32_UserProfile -ComputerName $hostname | Format-List LocalPath,LastUseTime}
  • 有效,但间隔有趣且具有奇怪的 runspaceID 项目:{Invoke-Command -ComputerName $hostname -HideComputerName -ScriptBlock {Get-WMIObject Win32_UserProfile | Select-Object LocalPath,LastUseTime}}

解决方法

您的代码有语法错误,缺少关闭开关的大括号。将文本输出与对象输出混合会导致 powershell 出现问题。例如,这工作正常。

function Show-Menu {Write-Host "
    1)Option A
    2)Option B
    3)User Profiles of Remote PC
"}

DO {
#    Show-Menu
#    $UserChoice = Read-Host "Enter # of tool you want to run"
#    $hostname=Read-Host "enter hostname"

  $hostname = 'comp001'
  switch (3) {
        1 {'You choose opt1'}
        2 {'You choose opt2'}
        3 {Get-CimInstance Win32_UserProfile -CimSession $hostname | 
             Select-Object -Property LocalPath,LastUseTime}
  }
} UNTIL (1) 
,

正如我所提到的,没有建立供您指出的 cimsession。因此,让我们使用 New-CimSession$hostname 中提供的计算机名称创建它。

function Show-Menu 
{
Write-Host "
    1)Option A
    2)Option B
    3)User Profiles of Remote PC
"
}

Do {

    Show-Menu
    $User_Choice = Read-Host -Prompt "Enter # of tool you want to run"
        switch ($User_Choice) {

            1 {'You choose opt1'}
            2 {'You choose opt2'}
            3 {

                $hostname = Read-Host -Prompt "Enter Computer Name"
                    if ([string]::IsNullOrEmpty($hostname) -eq $true) {
                        "No Computer Name was specified";
                        Break
                    }

                    try {
                        
                        $CIMSession = New-CimSession -ComputerName $hostname -ErrorAction stop

                        Get-CimInstance -ClassName Win32_UserProfile -CimSession $CIMSession | Select-Object -Property LocalPath,LastUseTime 

                    }
                    Catch [Microsoft.Management.Infrastructure.CimException] {
                        $Error[0].Message.Split('.')[1].Trim()

                    }
                    Finally {
                        if (Get-CimSession) {
                            Get-CimSession | Remove-CimSession
                            
                        }
                    } 
                }
        }

} Until ($User_Choice -notcontains '')

除了一些小的语法问题外,您应该在 #3 选择中包含 $hostname 提示。除非,您也想将该变量用于其他选择。当然,您需要一些错误处理,以防在连接到机器时发生错误,我们可以使用 try{}catch{} 块;添加了一个 finally{} 块,用于清除 cimsessions。