0

Cmdlet below works normally, but does nothing within a do & switch statement in code block at bottom? Debugging in ISE doesn't provide any help. Removing | Select-Object does make it function, but produces too much info. Removing -CimSession $hostname does make it function. So issue seems related to the remote PC and/or SELECT statement.

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 '')
  • Same issue with this cmdlet: {Get-WMIObject Win32_UserProfile -ComputerName $hostname | Select-Object -Property LocalPath,LastUseTime}
  • Works, but is spaced funny: {Get-WMIObject Win32_UserProfile -ComputerName $hostname | Format-List LocalPath, LastUseTime}
  • Works, but is spaced funny & has weird runspaceID item: {Invoke-Command -ComputerName $hostname -HideComputerName -ScriptBlock {Get-WMIObject Win32_UserProfile | Select-Object LocalPath, LastUseTime}}
gregg
  • 1,084
  • 1
  • 12
  • 25
  • That's because you're pointing to a non existing *cimsession*. Create the cim session first, then run it against that, or just swap the parameter to `-ComuterName`. I would swap out the `-eq` to `-ne $null` – Abraham Zinala Jun 13 '21 at 17:09
  • @AbrahamZinala What do you mean non-existing session? How do I create it first? Changing it to `-ComputerName $hostname` had the same issue. Any idea WHY `Format-List` works, but `Select-Object` doesn't? – gregg Jun 13 '21 at 17:27
  • @js2010 fixed missing curly bracket, it was a poor copy/paste, its in the original code & wasn't the cause. What do you mean by a syntax error & your last sentence? I'm new to powershell & scripting – gregg Jun 13 '21 at 17:31
  • I switched it to `-NotContains` as I kept getting issues with `-eq`, and `-ne`. – Abraham Zinala Jun 13 '21 at 18:09

2 Answers2

0

Your code has a syntax error and is missing a curly bracket to close the switch. Mixing text output with object output causes problems in powershell. For example, this works fine.

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) 
js2010
  • 23,033
  • 6
  • 64
  • 66
  • While your code works, I need to prompt for hostname & that is when it fails. Is the hostname user input variable filling done wrong & my problem? Can you update your code to do this however you see fit? – gregg Jun 13 '21 at 17:42
  • 1
    I wouldn't use prompts at all. Just have a function with the options you want, and only display the get-ciminstance output. – js2010 Jun 13 '21 at 17:54
0

As I mentioned, theres no cimsession thats been established for you to point to. So, lets create it using New-CimSession and the computer name provided in $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 '')

Besides some minor syntax issues, you should have the $hostname prompt inside your #3 selection. Unless, you'd like to use that variable for the other selections as well. And of course, you want some error handling in case an error occurs connecting to the machine which we can do with a try{} and catch{} block; added a finally{} block for cimsessions clean up.

Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
  • It surprised me too but it works without new-cimsession. – js2010 Jun 13 '21 at 19:28
  • @js2010, just tried it, I guess it does. That's odd. From the MSDocs it reads it creates a local *cim session* if `-computername ` wasn't specified. Guess it does it for remote computers too? Weird stuff lol – Abraham Zinala Jun 13 '21 at 19:43