Search code examples
powershellauthenticationsccminvoke-command

Powershell VSCode invoke-command from localhost different user for SCCM


Because VSCode is not able to run a powershell console and debug it as a different user i am trying to get arround it with invoked credentials like this:

Start-Service -Name "WinRM"
$cred = Get-Credential -Credential domain\myuser
Invoke-command -Credential $cred -Computer "localhost" -scriptblock {

    Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1"
    Set-Location 'XXX:' # my sccm site code
    Import-CMComputerInformation -CollectionName "All Systems" -ComputerName "TestComputer" -MacAddress "00:00:00:00:00:69"

}

If i start it in the debugger of VSCode (F5) it starts but cant connect then to the SCCM Server infrastructure. Could someone help me to solve this issue?

Error:

Cannot find drive. A drive with the name 'XXX' does not exist.
    + CategoryInfo          : ObjectNotFound: (XXX:String) [Set-Location], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
    + PSComputerName        : localhost

This command cannot be run from the current drive. To run this command you must first connect to a Configuration Manager drive.
    + CategoryInfo          : DeviceError: (Microsoft.Confi...ormationCommand:ImportComputerInformationCommand) [Import-CMComputerInformation], InvalidOperationException
    + FullyQualifiedErrorId : CommandCannotExecuteFromCurrentDrive,Microsoft.ConfigurationManagement.Cmdlets.Oob.Commands.ImportComputerInformationCommand
    + PSComputerName        : localhost

If i logoff from my machine and login with my admin credentials and execute everything in the invoke-command scriptblock it works. As i am not allowed to work like this by our company policy's is there maybe a alternative way or something i can do to use the visual studio code debugger?


Solution

  • Have you logged onto the SCCM site server interactively with the credentials you are using and opened the console at least once? I believe this initial first opening is required before the drive is accessible remotely...