Don't use Invoke-Command, just launch a separate powershell process with the users credentials.
https://www.itdroplets.com/run-a-command-as-a-different-user-in-powershell/
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am using powershell to roll out a complete set of users, applications and servers to new server machines, for our software we require some environment variables to be set in the runtime users context. to do so, i wanted to run a bash script that rolls out the EnvVars but this must be done by the user himself. So i thought i could just run this by using Invoke-Command ComputerName localhost... but this always results in an error message:
[localhost] Beim Verbinden mit dem Remoteserver "localhost" ist folgender Fehler aufgetreten: Zugriff verweigert Weitere Informationen finden Sie im Hilfethema
"about_Remote_Troubleshooting".
+ CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
(sorry, thats a german machine in that case)
this is the script sniplet i try to use:
Write-Host seting up Environment
$usernameIA = "NameOfUser"
$password = 'ThePassword'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($usernameIA, $securePassword)
Invoke-Command -ComputerName localhost -Credential $credential -ScriptBlock $ScriptBlock
where the User has been just created a bit earlier in the script, so it has not yet been loggen on.
i have read somewhere that it works with PS7, but i can not always update to PS7 and have to go ith built in PS5 due to these production machines do not have access to the internet.
am i doing somethjing wrong or am i missing something? is there a better way to roll out environment variables to the newly created users?
Don't use Invoke-Command, just launch a separate powershell process with the users credentials.
https://www.itdroplets.com/run-a-command-as-a-different-user-in-powershell/
Assuming you want to set those environment variables so they persist, you can use this in the PowerShell script:
[System.Environment]::SetEnvironmentVariable('Name','Value')
You can add a 3rd parameter to control which set of environment variables (Machine, Process, or User).
https://learn.microsoft.com/en-us/dotnet/api/system.environmentvariabletarget?view=netframework-4.6