Remote desktop powershell indication - powershell

I'm trying to automate the remote desktop process (from windows client to windows server) for a service I'm working on.
To do so, I'm using the following Powershell script:
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
$Process = New-Object System.Diagnostics.Process
$ProcessInfo.FileName = "$($env:SystemRoot)\system32\cmdkey.exe"
$ProcessInfo.Arguments = "/generic:TERMSRV/$ComputerCmdkey /user:$User /pass:$Password"
$ProcessInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
$Process.StartInfo = $ProcessInfo
if ($PSCmdlet.ShouldProcess($ComputerCmdkey,'Adding credentials to store')) {
[void]$Process.Start()
}
$ProcessInfo.FileName = "$($env:SystemRoot)\system32\mstsc.exe"
$ProcessInfo.Arguments = "$MstscArguments /v $Computer"
$ProcessInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Normal
$Process.StartInfo = $ProcessInfo
if ($PSCmdlet.ShouldProcess($Computer,'Connecting mstsc')) {
[void]$Process.Start()
}
This script works perfectly fine. What i'm missing is an indication on the client side (other than the UI window - some return value or event log) that can tell me whether the connection was successful or did it fail.
Can anyone here help with it?
Thanks

Related

Execute drive cleanup command for remote server powershell

I have the below code for cleaning space in C drive. It is working fine locally but I have to execute it for remote servers
$ProcessInfo = New-Object -TypeName System.Diagnostics.ProcessStartInfo
$ProcessInfo.FileName = "$env:SystemRoot\system32\dism.exe"
$ProcessInfo.Arguments = "/Online /NoRestart /Cleanup-Image /StartComponentCleanup"
$ProcessInfo.UseShellExecute = $true
$ProcessInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Minimized
$Process.StartInfo = $ProcessInfo
$Process.Start() | Out-Null
Please let me know how to execute this for remote server using Invoke-command or some other way

Can I change powershell version to 7 with "Run PowerShell script" action on Power Automate Desktop?

Can I change powershell version to 7 on "Run PowerShell script" action?
If I can, how?
I know this post but I couldn't find if I can change the version PAD execute.
https://powerusers.microsoft.com/t5/Power-Automate-Desktop/Powershell-and-other-script-SUPPORTED-VERSION/td-p/1501322
I already installed powershell version7.
I'd like to use "-UseQuotes" option on "Export-Csv".
FYI, my PSVersion is here.
PSVersion 5.1.19041.1320
Thank you,
I also checked registry about PAD but There is noregistry to manage powershell
This is a bit ugly but it will get the job done.
I have this very basic flow, it just runs the PS script and outputs the results in a message box.
Throw this into the PowerShell task and then view the output.
$psVersion = (Get-Host).Version.Major
if ($psVersion -ne 7) {
$processInfo = New-Object System.Diagnostics.ProcessStartInfo
$processInfo.FileName = "C:\Program Files\PowerShell\7\pwsh.exe"
$processInfo.RedirectStandardError = $true
$processInfo.RedirectStandardOutput = $true
$processInfo.UseShellExecute = $false
$processInfo.Arguments = $MyInvocation.MyCommand.Definition
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $processInfo
$process.Start() | Out-Null
$process.WaitForExit()
$stdout = $process.StandardOutput.ReadToEnd()
$stderr = $process.StandardError.ReadToEnd()
Write-Host $stdout
} else {
# Your logic goes here!
Write-Host "PowerShell Version = $psVersion"
}
You should get this ...
Essentially, it's checking for the version and if it's not version 7, it will launch PowerShell 7 and then retrieve the output.
Take note of the file path for PS7, you may need to change that or you may be able to simplify it. I've gone with the full path to make sure it works.

Installing an exe with PowerShell DSC returns exit code only when run through LCM

I am trying to install the HPC Pack 2012 R2 U3 setup using PowerShell DSC. The following code works and installs the software:
$HpcPackName = "Microsoft HPC Pack 2012 R2 Server Components"
$HpcPackSourcePath = "C:\Temp\HPC2012R2_Update3_Full\setup.exe"
$sqlServer = "EMEAWINQA15"
$Arguments = "-unattend -headNode"
function InstallUsingProcess
{
[CmdletBinding()]
param()
Write-Verbose "HpcPackSourcePath: $HpcPackSourcePath"
Write-Verbose "Arguments: $Arguments"
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = $HpcPackSourcePath
$startInfo.Arguments = $Arguments
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$exitcode = 0
$process.Start() | Out-Null
$process.WaitForExit()
if($process)
{
$exitCode = $process.ExitCode
Write-Verbose "Exit code: $exitCode"
}
}
InstallUsingProcess -Verbose
However, when I run the same thing using a Script DSC configuration, it succeeds but returns very quickly with exit code 10:
Configuration TestHpcInstall
{
Import-DscResource –ModuleName PSDesiredStateConfiguration
Node $AllNodes.Where({$_.Roles -contains 'HpcHeadNode'}).NodeName
{
$HpcPackName = "Microsoft HPC Pack 2012 R2 Server Components"
$HpcPackSourcePath = "C:\Temp\HPC2012R2_Update3_Full\setup.exe"
$sqlServer = "EMEAWINQA15"
$Arguments = "-unattend -headNode"
Script TestInstall
{
GetScript = {
return #{ "Result" = "$true"}
}
TestScript = {
return $false
}
SetScript = {
Write-Verbose "HpcPackSourcePath: $using:HpcPackSourcePath"
Write-Verbose "Arguments: $using:Arguments"
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = $using:HpcPackSourcePath
$startInfo.Arguments = $using:Arguments
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$exitcode = 0
$process.Start() | Out-Null
$process.WaitForExit()
if($process)
{
$exitCode = $process.ExitCode
Write-Verbose "Exit code: $exitCode"
}
}
}
}
}
TestHpcInstall -ConfigurationData $configData -OutputPath "C:\Temp"
Start-DscConfiguration -ComputerName "EMEAWINQA15" -Path "C:\Temp\" -Verbose -Wait -Force
This is the same code used by the Package resource, which fails because error code 10 is returned instead of 0 (which is the case when the package installs successfully, as in the top-most code sample). The setup does not produce any output or log file.
Any ideas? I'm stumped.
I found the problem. I thought this had something to do with permissions because the setup gives a UAC elevation prompt when run normally. However, I had crossed it out before for two reasons:
LCM runs under NT AUTHORITY\SYSTEM account and is therefore an administrator, and
Because I supplied local administrator credentials to the Package resource already like so
(which didn't work):
Package InstallHpcHeadNode
{
Ensure = "Present"
Name = $HpcPackName
ProductId = ""
Path = $HpcPackSourcePath
Arguments = $Arguments
Credential = (Get-Credential)
}
But this was a mistake. From the docs, the Credential property says:
Provides access to the package on a remote source. This property is
not used to install the package.
which I'll admit I overlooked. I should have instead used the PsDscRunAsCredential property to force the installation using the supplied credentials. Still don't know why the installer doesn't run under NT AUTHORITY\SYSTEM though.

Powershell start IE in private and log in to page

I'm trying to automate a process of activating accounts by logging in to email, I have it working when I am not on the business network with the following code.
$username = "user"
$password = "pass"
$url = "url"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate($url)
while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}
$usernamefield = $ie.Document.getElementByID('UsernameTextBox')
$usernamefield.value = $username
$passwordfield = $ie.Document.getElementByID('PasswordTextBox')
$passwordfield.value = $password
$ie.document.getElementById("ctl00_ContentPlaceHolder1_SubmitButton").click()`
My issue is that once connected to the business network it then uses SSO to log in so I do not get the option to put in a username and password. To be able to put in a username and password I need to start IE in private mode. I have been able to start IE in private with the start-process command but I cannot find a way to select the window to type in the username and password.
Is there a way I can use powershell to log in to the website in a private browser?
You can get a handle to the private IE by starting it with start-process, as you referred to.
Start-Process -FilePath "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -ArgumentList ' -private http://bogus.bogus'
Then you can connect to it by finding it -
$Shell = New-Object -Com Shell.Application
$apps = $shell.windows()
$ie = $apps | where { $_.locationname -eq 'http://bogus.bogus/' }
Happy scripting. :)

Trying to do mstsc remotely using powershell doesn't work

I have two server as X and Y. I have a powershell script that I am using for having a remote desktop of X on Y. The powershell script that I have to run on Y is --
$hostname = 'X'
$User = 'u-name'
$Password = 'password'
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
$Process = New-Object System.Diagnostics.Process
$ProcessInfo.FileName = "$($env:SystemRoot)\system32\cmdkey.exe"
$ProcessInfo.Arguments = "/generic:TERMSRV/$hostname /user:$User /pass:$Password"
$Process.StartInfo = $ProcessInfo
$Process.Start()
$ProcessInfo.FileName = "$($env:SystemRoot)\system32\mstsc.exe"
$ProcessInfo.Arguments = "$MstscArguments /v $hostname"
$Process.StartInfo = $ProcessInfo
$Process.Start()
When I run this script locally on Y, it does run and opens the server X in Y.
But I want to trigger it from X only to open X in Y. So I Invoke this powershell script from X as --
$pass = ConvertTo-SecureString -AsPlainText test-password -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList test-uname,$pass
$hostn = hostname
$Use = 'u-name'
$Pass = 'password'
Write-Host "$hostname"
$ScriptBlockContent={
Param ($hostname,$User,$Password)
E:\Script\test.ps1 $hostname $User $Password}
Invoke-Command -ComputerName Y -Credential $cred -Scriptblock $ScriptBlockContent -ArgumentList $hostn,$Use,$Pass
When I am invoking this. It does open mstsc.exe on Y but only for some fraction of seconds and doesn't open the server X on Y. Can somebody please help.. !!
Thanks.
You are trying to launch an application with a GUI in a remote powershell session which has no desktop/display. Even if you use the credentials of a logged in user, the things you launch through Invoke-Command will not be visible to the logged-in user's session.
I think that this is possible with PsExec.exe, but I can't confirm.