remote powershell in azure VM - powershell

Since many days I am trying to sysprep an azure virtual machine.
Following is the command that I am using to connect to the VM from local and executing the Upload-GoldImage.ps1 script
Invoke-Command -ConnectionUri $uri -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -Credential $credential -ScriptBlock {C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -file "c:\program files\WindowsPowershell\Upload-GoldImage.ps1" -validateCurrentOS}
remote machine gets connected successfully. All the commands in the ps1 file are executed successfully but only the sysprep command isn't executing.
The ps1 mentioned above contains one command to sysprep the machine - Start-Process -FilePath C:\Windows\System32\Sysprep\sysprep.exe -ArgumentList '/generalize /oobe /shutdown /quiet'
Write-Host("After sysprep")
When this gets executed on my local powershell prompt - "After sysprep" is getting printed. I am getting no error in the sysprep command. Still the machine is not getting sysprepped.
Please if anyone could help me out
Thanks.

Finally I was able to sysprep the machine from my local powershell.
Previously my script used Start-Process command.
But then, http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx#Diagnostics_Process_Start
- this link helped me out, where I tried various several ways mentioned in the link.

Related

How to Remotely Invoke Applications (.exe file) with PowerShell with remote pc on windows?

I'm trying to execute following command on remote server:
Import.exe. file resides on remote server and this command is completely working on remote server's command line. But when I try to execute it from local system its not showing any error but still not producing any output.
Here is the command :
Start-Process -NoNewWindow -FilePath "C:\\V11\\IMPORT.EXE" -ArgumentList "cutrite1620626276329","/AUTO","/OVERWRITE"
I have connected to remote pc using ssh command.
Note : Both pc, mine and remote pc, are windows.
Start-Process command will execute process on localhost. (More on that here:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-5.1)
What you want to do is invoke this command in PSRemoting session like:
Invoke-Command -ComputerName "COMPUTERNAME" -ScriptBlock {
#This command will be executed on machine COMPUTERNAME
Start-Process -NoNewWindow -FilePath "C:\\V11\\IMPORT.EXE" -ArgumentList "cutrite1620626276329","/AUTO","/OVERWRITE"
};
There are several ways to access psremoting (you can read it here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote?view=powershell-5.1)

Powershell : I am trying to run a script on a remote server. The script is for gathering info of that remote and other servers in the same domain

Editing to be clear with the issue
I am running the below code from a local computer which should run the script Test.ps1 placed on $server at C:\Temp to collect data from $server and other servers.
When i perform this, the script runs but i only get data for the $server and not the others. While if i run the C:\Temp\Test.ps1 script sitting on the $server, i get the desired output for all servers.
I am using this code
$s = New-PSSession -ComputerName $server -Credential $credential
Invoke-Command -Session $s -Command {C:\Temp\Test.ps1}
I am getting the output for the remote server on which the code is placed (localhost for the script), buti am not getting any output for the other servers.
While if I run the script locally on the remote server i get output from all the servers
The script does not have to be on the remote host. PowerShell allows you to run local PS1 scripts on remote computers.
Running Remote Commands
https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-7.1
Run a Script
To run a script on one or many remote computers, use the FilePath
parameter of the Invoke-Command cmdlet. The script must be on or
accessible to your local computer. The results are returned to your
local computer.
For example, the following command runs the DiskCollect.ps1 script on
the remote computers, Server01 and Server02.
Invoke-Command -ComputerName Server01, Server02 -FilePath 'c:\Scripts\DiskCollect.ps1'
As for this...
'My requirement is to run that script from the remote host per
security protocols'
... just use a script block as shown via the same MS Docs link
Run a Remote Command
To run a command on one or more computers, use the Invoke-Command
cmdlet. For example, to run a Get-UICulture command on the Server01
and Server02 remote computers, type:
Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {& .c:\Scripts\DiskCollect.ps1}
Of course, I just removed Get-UICulture cmdlet in the docs command, with a script to use.
It depends on how your remote script uses credentials to access other servers. This should classify as a double hop issue.
If you cannot use CredSPP, you can use Resource-Based Kerberos Constrained Delegation
or use credentials inside the Invoke-Command scriptblock i.e. you can define a credential object on the local computer to be used in the -credentials parameter, and refer to it with $using:cred in your scriptblock.
The article does a good job of summarizing all your options.

Start explorer.exe remotely with a path specified in Powershell

The problem I have is that I am able to Invoke-command explorer.exe on a remote machine without giving it any path parameters, yet when I enter:
Invoke-Command -ComputerName PC01 -Credential $cred -ScriptBlock {explorer.exe "C:\Foldername"}
Nothing happens, except for the fact that I get an error entry in the logs saying:
The server {75DFF2B7-6936-4C06-A8BB-676A7B00B24B} did not register with DCOM within the required timeout.
First thing, If you are trying this directly on the local system, the GUI will pop up properly.
Invoke-Command -ScriptBlock {C:\Windows\explorer.exe "C:\folder"}
But the problem, is how powershell will open a GUI console invoked from the remote system. Basically, it does not have the session to hold. You need a desktop session to do that.
In that case, you should use PSEXEC with -i
psexec -i -d -s c:\windows\explorer.exe C:\folder
Download it from Here: PSExec-v2.11. This link has all the explanations with examples on how to use each utility.
Hope it helps.

New-MailboxExportRequest error when running from CMD

I have a very simple PowerShell script that runs the new-mailboxexportrequest command. if I run the script as a scheduled task, I get the following error:
new-mailboxexportrequest : Failed to communicate with the mailbox database.
If I open the PowerShell command prompt, and run the script, I get the same error.
The script only works when I manually open the exchange management shell and run the script.
Keep in mind that the first line of the script actually has the exchange snapin command:
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
The question is, what loads from the exchange management shell that doesn't load on when the snapin runs?
Also, the scheduled task, the manual powershell command and the exchange management shell commands are running on the same box (exchange server). I'm also running them with the same user account.
Any help is appreciated.
the reason for your problem is explained in this link:
https://blogs.technet.microsoft.com/rmilne/2015/01/28/directly-loading-exchange-2010-or-2013-snapin-is-not-supported/
this should fix your problem :
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<ServerFQDN>/PowerShell/ -Authentication Kerberos
Import-PSSession $Session
you can check this also :
powershell -psconsolefile "C:\Program Files\Microsoft\Exchange Server\Bin\exshell.psc1" your-exchange-command

Cannot find path 'SharePath' because it does not exist

I am running command to copy a file from shared location to the local machine.
Command is Copy-Item '\\ServerName\share\Setup\Setup.msi' 'C:\Windows\Temp\RiversandSetup'
This command is works fine when I run it from Server1. But when i run same command from remote machine by opening a session it fails with error 'Cannot find path 'SharePath' because it does not exist.'. Command is $sessions = New-PSSession -ComputerName RemoteServerName
Invoke-Command -session $sessions -ScriptBlock {Copy-Item '\\SharePath\share\Setup\Setup.msi' 'C:\Windows\Temp\RiversandSetup'}
Please advice.
You are most likely running into an issue with double-hop authentication. This could help but it depends on your environment:
Setting up CredSSP properly for powershell multi-hop issue