locked
invoke-command and new-object RRS feed

  • Question

  • Hi, I am trying to find information on a remote machine.  So I am playing around with Invoke-Command.

    This code works:

    $pc = Read-Host "Enter machine name"
    Invoke-Command -ComputerName $pc -ScriptBlock {
    $software = Read-Host "Enter software to search for"
    Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -like "*$software*" } | Select-Object DisplayName, DisplayVersion, UninstallString 
    Write-host "your software is $software" }

    When I run this it will prompt me and then shows me the software I'm searching for on the remote machine.

    The following code gives no errors but it also gives no data.


    Invoke-Command -ComputerName test-pc4 -ScriptBlock {
    $oneSignAuth = New-Object -ComObject "OneSignAuth.OneSignAuth"
    $currentOneSignUser = $oneSignAuth.CurrentOneSignUser
    $currentOneSignUser.Username | c:\temp\one.txt}

    One.txt does get created on test-pc4 but it is an empty file. One.txt should have my login ID.      Is Invoke-Command unable to run New-Object on remote machines?    
    Thursday, June 8, 2017 3:57 PM

All replies

  • Try Out-File C:\temp\one.txt

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, June 8, 2017 4:01 PM
  • Hi,

    1) you need to enable remote PowerShell on remote server using following command

    Enable-PSRemoting -Force

    2) use Get-Credential command to obtain username and password of remote server

    for example use can use a command like below
    Invoke-Command -ComputerName 192.168.0.8 -ScriptBlock { Get-Service } -credential Administrator

    Thanks,

    NTRao

    ______________________________________

    Mark as answer if it helps!

    Thursday, June 8, 2017 4:08 PM
  • Hi,

    1) you need to enable remote PowerShell on remote server using following command

    Enable-PSRemoting -Force

    2) use Get-Credential command to obtain username and password of remote server

    for example use can use a command like below
    Invoke-Command -ComputerName 192.168.0.8 -ScriptBlock { Get-Service } -credential Administrator

    Thanks,

    NTRao

    ______________________________________

    Mark as answer if it helps!

    The OP is not having any issues with Invoke-Command, so this is irrelevant.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, June 8, 2017 4:13 PM
  • Hi.  that was a type-o.  I changed it to Out-File C:\temp\one.txt and it still is an empty file.   Yes, I have enabled PSRemoting on all systems via GPO.   Invoke-Command does work but not for the code I'm trying which uses New-Object.

    I have administrative rights on all systems in our environment so I should not need get-credential

    Thursday, June 8, 2017 4:14 PM
  • I would first start off with verifying that $oneSignAuth.CurrentOneSignUser actually returns a value

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, June 8, 2017 4:34 PM
  • You could probably try appending a static text to the same file to confirm whether $OneSignAut.CurrentoneSignUser returns some value

    Invoke-Command -ComputerName test-pc4 -ScriptBlock {
    $oneSignAuth = New-Object -ComObject "OneSignAuth.OneSignAuth"
    $currentOneSignUser = $oneSignAuth.CurrentOneSignUser
    $currentOneSignUser.Username | Out-File c:\temp\one.txt
    'Second line' | Out-File c:\temp\one.txt -Append
    $currentOneSignUser.Username -eq $null | Out-File c:\temp\one.txt -Append
    }


    Regards kvprasoon

    Thursday, June 8, 2017 5:06 PM
  • Hi thanks.   It does equal NULL since that returned TRUE to the .txt file.  But it should return my login ID.  If I take the same code and I run it on TEST-PC4 from PowerShell ISE then it works and c:\temp\one.txt has my login ID.     Example:

    $oneSignAuth = New-Object -ComObject "OneSignAuth.OneSignAuth"
    $currentOneSignUser = $oneSignAuth.CurrentOneSignUser
    $currentOneSignUser.Username | Out-File c:\temp\one3.txt
    'Second line' | Out-File c:\temp\one.txt -Append
    $currentOneSignUser.Username -eq $null | Out-File c:\temp\one4.txt -Append

    one3.txt has my user ID

    one4.txt says FALSE

    Thursday, June 8, 2017 6:01 PM
  • yes i agree with you Enable-PSRemoting -Force is not related with inovake-command, but in this case he is trying to access remote machine, so we need to enable remote powershell using Enable-PSRemoting -Force

    Hope this make sense !

    Anyways thanks for your comment and appreciate your response !

    NTRao

    Friday, June 9, 2017 7:08 AM
  • Hi thanks.   It does equal NULL since that returned TRUE to the .txt file.  But it should return my login ID.  If I take the same code and I run it on TEST-PC4 from PowerShell ISE then it works and c:\temp\one.txt has my login ID.     Example:

    $oneSignAuth = New-Object -ComObject "OneSignAuth.OneSignAuth"
    $currentOneSignUser = $oneSignAuth.CurrentOneSignUser
    $currentOneSignUser.Username | Out-File c:\temp\one3.txt
    'Second line' | Out-File c:\temp\one.txt -Append
    $currentOneSignUser.Username -eq $null | Out-File c:\temp\one4.txt -Append

    one3.txt has my user ID

    one4.txt says FALSE

    did these codes works on local?

    $oneSignAuth = New-Object -ComObject "OneSignAuth.OneSignAuth" $currentOneSignUser = $oneSignAuth.CurrentOneSignUser $currentOneSignUser.Username | Out-File c:\temp\one3.txt

    ........


    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, June 9, 2017 9:11 AM
  • Yes, if I run it on my target machine locally it works.  It only fails when run via invoke-command.  
    Friday, June 9, 2017 1:11 PM
  • Yes, if I run it on my target machine locally it works.  It only fails when run via invoke-command.  

    I suggest you make these codes as a .ps1 file then invoke it through this cmdlet: Invoke-Command.

    Besides, workaround is using task jobs or GPO to do this.

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, June 13, 2017 4:22 AM
  • Hi,

    Try to run Invoke-Command with credentials. Also check security of your COM object using Dcomcnfg.exe

    BR Sergei.

    • Proposed as answer by Hello_2018 Tuesday, June 13, 2017 9:46 AM
    • Unproposed as answer by jrv Tuesday, June 13, 2017 9:47 AM
    Tuesday, June 13, 2017 9:41 AM
  • The COM object OneSignAuth.OneSignAuth cannot be remoted as there is no interactive user. To learn more about how to use this object post in the Google developers forums.


    \_(ツ)_/

    • Proposed as answer by Hello_2018 Thursday, June 22, 2017 7:06 AM
    Tuesday, June 13, 2017 9:47 AM
  • Hi bellmonster,

    did this issue was resolved?

    Best regards

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, June 15, 2017 1:53 AM