Remotely running Acrobat Distller via powershell command line - powershell

I have a task to remotely run Acrobat Distller (AD) remotely.
I was able to locally run AD in the command prompt:
"C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\acrodist.exe" /O $OutputFolder $InputFolder\test.ps
I tried invoking the same command using powershell:
powershell.exe -NoExit -Command Invoke-Command -ComputerName $server -ScriptBlock {'"C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\acrodist.exe" /O $OutputFolder $InputFolder\test.ps'}
When ran, the powershell command did prompt any error BUT it did not generate the expected PDF output as well.
Can I get some assistance on what I am doing wrong here?
Thanks

You probably need to use $using:OutputFolder and $using:InputFolder.
Normally, powershell variables are only set for your session and don't carry over to remote servers. The $Using: format allows you to do this with Invoke-Command.

Related

Unable to run the parametrized batch file on remote machine using PowerShell

Execute the remote server parametrized batch file from PowerShell.
Doesn't throw an error nor executed command on remote machine.
$path = "D:\run\test-5.2.bat";
Invoke-Command -ComputerName testserver -Scriptblock { "$path" }
Script inside the bat file is msiexec with parameters, which shall execute through Command Prompt only.
Based on this msdn link, you can run a ps1 script file on remote computers. So if it is possible to "port" the content of the bat file in a ps1 it should work. Here is the msdn example:
Example 11: Run a script on all the computers listed in a text file
PS C:\> Invoke-Command -ComputerName (Get-Content Servers.txt) -FilePath C:\Scripts\Sample.ps1 -ArgumentList Process, Service
This example uses the Invoke-Command cmdlet to run the Sample.ps1 script on all of the computers listed in the Servers.txt file. The command uses the FilePath parameter to specify the script file. This command lets you run the script on the remote computers, even if the script file is not accessible to the remote computers.
When you submit the command, the content of the Sample.ps1 file is copied into a script block and the script block is run on each of the remote computers. This procedure is equivalent to using the ScriptBlock parameter to submit the contents of the script.
Hope that helps
$path is a string. PowerShell simply echoes bare strings instead of executing them, unlike CMD or bash. Use the call operator (&):
& "$path"
or Start-Process:
Start-Process cmd.exe -ArgumentList '/c', $path -NoNewWindow -Wait
to have PowerShell execute a string as a command. Since you say you're running msiexec.exe from the batch script using the latter may be required.
On top of that you have a scope issue. The variable $path inside the scriptblock is not the same as the one in the global scope. You can mitigate that via the using: scope qualifier:
Invoke-Command -Computer testserver -Scriptblock { & "$using:path" }
or by passing $path as an argument to the scriptblock:
Invoke-Command -Computer testserver -Scriptblock { & "$($args[0])" } -ArgumentList $path

Installing an MSI file on a remote machine with PowerShell

I'm working on a utility to automate some processes and one task is to install a .msi file on a remote machine. The file is found in C:\Users\username on the remote machine and for simplicity's sake, the filename is file.msi. The command I'm using is:
Invoke-Command -ComputerName $remoteMachine -ScriptBlock{cmd /c start /wait msiexec /i $installPath /quiet}
When I execute this on my local dev machine, it doesn't show any errors, but doesn't install the file.
However, when I copy the exact command inside the brackets and run it in a PowerShell script on the remote machine, it installs successfully. I know my $remoteMachine is correct because I use it extensively throughout the rest of the script.
I know the $installPath variable also isn't the issue because for testing purposes I hardcoded the full path and it still doesn't install.
I also have proper permissions on the remote machine because earlier in the script I copy and paste the .msi from one machine to another without a problem.
I've tried a combination of commands and have been stuck here for a while, so any help would be greatly appreciated!
Ideally, this should work.
Invoke-Command -ComputerName $remoteMachine -ScriptBlock{msiexec /i $installPath /quiet}
The reason it is failing is coz you are not passing the $installPath as argumentlist. Modify it like this.
Invoke-Command -ComputerName $remoteMachine -ScriptBlock{
param(
[Parameter(Mandatory=$true,
Position=0)]
$installPath
)
cmd /c start /wait msiexec /i $installPath /quiet
} -ArgumentList $installPath
But if it isn't working, here is a workaround that I used a while ago.
Create a .bat file with the command msiexec /i $installPath /quiet and push it to the location just like you pushed the msi file.
Now from the invoke scriptblock, simply call the bat file instead.
Invoke-Command -ComputerName $remoteMachine -ScriptBlock{C:\Users\Username\Install.bat}
where Install.bat is the name of your bat file.
Note: You might want to use the /norestart switch as well if you are not looking to cause a reboot. Depends on what you are trying to install.
Beginning in PowerShell 3.0, you can use the Using scope modifier to identify a local variable in a remote command.
syntax of Using :- $Using:<VariableName>
In your case :
Invoke-Command -ComputerName $remoteMachine -ScriptBlock{cmd /c start /wait msiexec /i $Using:installPath /quiet}
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-7.2#using-local-variables

Run PowerShell script as different user and hidden window from another PowerShell script

I need to run a PowerShell script from another PowerShell script as different user and hidden window.
What do I have so far:
Start-Process powershell '& C:\test.ps1' -WindowStyle Hidden -Credential $cred
where $cred is a PSCredential object. The test.ps1 script is for testing purposes one line with Remove-Item. As I can still see the file not being removed I can tell that the script is not being run.
EDIT
My original intention is to run regedit script but when I was implementing Start-Job answer (below) I've got this error:
Start-Job : The value of the FilePath parameter must be a Windows PowerShell script file. Enter th
e path to a file with a .ps1 file name extension and try the command again.
You could use jobs for this. As the Powershell documentation says
A Windows PowerShell background job runs a command without interacting
with the current session.
PS> $myJob = Start-Job -FilePath C:\test.ps1 -Credential $cred
To get the result/output of the job, use Receive-Job
PS> $myJob | Receive-Job -Keep
You can run the PowerShell scripts through task scheduler. I'm not sure if it is your goal, but it will let you execute PowerShell scripts on a user's session using their credentials.
I'll let you Google the how to, to fit your needs.

Run .bat file with Administrative rights

My PowerShell script runs a .bat file to install an .msu file. But I need to run this .bat file with Administrator rights.
The .bat file is:
WUSA C:\temp\Win8.1AndW2K12R2-KB3191564-x64.msu /quiet /norestart
I have Domain Controller and a lot of clients. With PowerShell PS session I interactively connect to every client. I need to use this bat file with Domain Admin credentials, how can I do this?
You could use Invoke-Command
You could save the servers in list in a text file and then use the Get-Content command to save the array in a variable:
$clients = Get-Content C:\ExampleClientList.txt
Then use the variable for the ComputerName parameter of Invoke-Command. Then in the scriptblock parameter is where you run the command, since you can run executables in PowerShell there isn't any need for the bat file. Last the Credential parameter will allow you run this as the Local administrator.
Invoke-Command -Computername $clients -ScriptBlock {WUSA C:\temp\Win8.1AndW2K12R2-KB3191564-x64.msu /quiet /norestart} -Credential (Get-Credential)
I am not sure i understand your question.
To start batch file from powershell you can use start-proccess command:
powershell start-process <path to your file> -verb RunAs

Batch file Calling a powershell script

Im trying to run a batch file on one server to call a powershell script on another server. The way that I need this to run is have the batch file on server 1 execute the powershell script on server 2 then have the powershell script output a file with the details onto server 1 where the batch file resides.
The code that I have for the batch file is as follows:
#ECHO OFF
SET PowerShellScriptPath="\\server1\c$\folder\Script.ps1"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList -File "\\server1\c$\folder\Script.ps1" -Verb RunAs}";
I need the script to run as admin from the batch file, however it fails and doesnt work and I cant seem to see why. Can anyone see why this isn't working?
Thanks