how to connect from powershell to powershell of another environment - powershell

i am runing powershell and from my powershell i want to connect to my Exchange powershell,
the diffrance betwwen the two files is in their target(right click =proproties),
"normal" powershell target ="%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe"
"exchange" powershell target = "C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'""
is there a way to connect to the exchange powershell by code??, my guess is to add those two extra line like in the target...

That command line will only work if the Exchange management tools are installed (it's going to try to load the Exchange management snapin.
Generally, it's easier to use implicit remoting in a script and import the functions from the remote shell into your current session:
http://www.mikepfeiffer.net/2010/02/managing-exchange-2010-with-remote-powershell/

Related

How to run .ps1 script using powershell 64bit exe via jenkins?

Powershell workflow is not supported in powershell 32bit mode. Thus i want to execute the script using powershell 64bit mode.How to run .ps1 script in 64bit mode via jenkins?
You could create your custom powershell step like in this stackoverflow answer
Executing powershell command directly in jenkins pipeline
You'll have to replace powershell.exe by the path to your powershell 64b exe
You could also use a shebang. some literature on that at How can I use a shebang in a PowerShell script?
it works by calling using c:\windows\sysnative\WindowsPowerShell\v1.0\powershell.exe instead of c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe
find more information about sysnative at https://www.thewindowsclub.com/sysnative-folder-in-windows-64-bit
I confirm that calling:
%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe -executionpolicy Unrestricted -file "%~dp0\file.ps1"
results in the 64 bit powershell.
One can validate inside their script by printing out debug of:
$GE_64BitPS_Status=[System.Environment]::Is64BitProcess
write-host $GE_64BitPS_Status

psexec trying to exec script error

I am learning how to interact with PowerShell and PsTools, and I have a problem with psexec.
I got a ps1 script named test.ps1 and inside it I have Get-Service which gives me the all services in my computer. Now I am going into PowerShell and go to c:\pstools. Then I type
psexec.exe C:\test\test.ps1
and it fails and returns me this error:
%1 is not a valid Win32 application
What could be the problem?
PsExec launches an executable. You need to specify the executable for PowerShell and associated arguments:
psexec.exe -accepteula -nobanner -s -h -d powershell.exe -ExecutionPolicy Bypass -NoProfile -NoLogo -File "C:\test\test.ps1"
The immediate answer to your question is:
psexec requires a (binary) executable as its first argument and cannot execute scripts directly.
Therefore, you must pass the Windows PowerShell executable name to psexec and in turn pass the desired script to the latter as an argument, via the -File parameter:
psexec powershell -File C:\test\test.ps1
That said, this particular use of psexec is pointless, as it would execute the script locally, as the current user, in which case use of psexec is a needless complication:
If you already know that, and the psexec command at hand is just a simplified example, never mind.
Otherwise, read on below.
The ps in psexec and PsTools has nothing to with PowerShell; PsTools is a collection of CLIs for managing Windows machines remotely, including processes, a common abbreviation of which is "ps", inspired by the standard ps Unix utility, which in turn inspired the initial tool in the collection, pslist; the primary purpose of psexec is to invoke arbitrary command lines on remote machines[1]
.
To invoke a PowerShell script locally:
From inside PowerShell itself, simply invoke the file path directly:
PS> c:\test\test.ps1
PS> & "c:\test\test.ps1", if the file path is / must be quoted or is provided via a variable or expression.
From outside of PowerShell, such as cmd.exe ("Command Prompt") or bash, you must invoke the PowerShell executable explicitly and pass it the script file path via the -File parameter:
Windows PowerShell: C:\> powershell -file c:\test\test.ps1
PowerShell Core: C:\> pwsh -file c:\test\test.ps1
In other words: the PowerShell's executable name is
powershell.exe for Windows PowerShell,
vs. pwsh for the cross-platform PowerShell Core edition (with extension .exe on Windows).
If you do need remote execution:
Pass \\-prefixed machine name(s) or IP address(es) to psexec; e.g., the following command executes the hostname utility on machine somemachine:
psexec \\somemachine hostname
There is no benefit to using psexec without targeting a different machine.[1]
However, psexec is normally not needed, because PowerShell has built-in support for remoting (i.e., the ability to execute commands on other machines; remoting requires setup, however - run Get-Help about_Remote_FAQ for more information); e.g., the equivalent of the above command is:
Invoke-Command -ComputerName somemachine { hostname }
[1] As TheIncorrigible1 points out, psexec can also be used for local execution as the system account (NT AUTHORITY\SYSTEM, the account that represents the computer as a whole) with the -s option.
Additionally, you can also run locally as another user, using the -u parameter - which, however, the standard runas utility can do as well (the latter doesn't offer passing the target user's password as a parameter for security reasons, but does offer to securely save a password for later reuse).
Run psexec -h for help.

Running SSIS Execute Process Task using Powershell ISE

I am trying to run an SSIS package via PowerShell ISE
Executable: C:\Windows\System32\WindowsPowershell\v1.0\PowerShell.exe
Arguments: N:\PowerShell\Move-Item.ps1
But the problem is the Arguments files is in the network drive which has no SSIS. My SSIS is in a Server called PFACESQAA. The Executable is from my network drive (Active Directory Domain). How do I run this from the Active Directory Domain or what is the best way to run this. I hope I made sense.
Thanks,
Powershell´s -file parameter works with network paths just fine, here is an example:
powershell.exe -executionpolicy bypass -file \\server\share\file.ps1

PowerShell: Starting the CLR Failed with HRESULT 8007000e

I'm getting the following error when running PowerShell scripts on a remote server:
Starting the CLR Failed with HRESULT 8007000e
This is basically how I'm running/calling the scripts:
On the local server I'm running a CMD script that calls a PowerShell script to create a remote session to a remote server. In the PowerShell script I also call a CMD script to run on the remote server like so:
$Script = [scriptblock]::create("cd $BuildPath | cmd.exe /c install.cmd $apptype")
The install.cmd script runs on the remote server and calls a PowerShell script that executes a series of tasks.
powershell ./Install.ps1 -BuildNum %BUILDNUM%
After the tasks are complete, the PowerShell script then calls another PowerShell script to run a separate series of tasks. This is when I hit the above error, when the second PowerShell script is called.
This is how the second PS1 script is called from the first PowerShell script:
powershell "& {. $BinToolsSrc\PostInstallValidation.ps1 -BuildNum $BuildNum -Test 'True'; Run-Validation -App $App -AppLoc $AppLoc -Env $Env:ENV -Site $Site -AppPool $AppPool -Config $Config -EnvConfig $EnvConfig -DllPath $DllPath}"
What usually causes the type of CLR error that I'm getting and how do I resolve it?
NOTE: I do not get this error when I run the install script locally on the remote server.
Thanks in advance!
UPDATE: Installing PowerShell 3 on the remote server seems to have solved the problem as it targets the .NET 4.0 runtime.
I too had the same problem because I have changed some path settings in my VScode unknowingly.
I have changed the settings to command prompt which works fine for me now...(This might not be the best solution though).screenshot

Powershell remotely register COM dll by using regsvr32

I found on Internet, this ps script may work. But the result I get is: no error pops up, but also DLL not found in registry after running the script.
Invoke-Command -ComputerName $servername -ScriptBlock {regsvr32.exe "\\uncpath\some.dll" }
I tried in both "run as administrator" and normal PS console window, and windows remote management service is on on remote server.
Any idea?
You need to use the silent option of regsrv32 (/s):
Syntax
REGSVR32 [/U] [/S] [/N] /I:[CommandLine] DLL_Name
Key /u Unregister Server.
/s Silent, do not display dialogue boxes.
/i Call DllInstall to register the DLL.
(when used with /u, it calls dll uninstall.)
/n Do not call DllRegisterServer, you must use this option
with /i.
CommandLine An optional command line for DllInstall
/c Console output (old versions only).