VMware Cloud Community
koolpavan007
Contributor
Contributor

How to set a PowerCLI script to run with Site Recovery Manger service account.

My PowerCLI script is below:-

Connect-VIServer 10.0.0.1

foreach($nic in (Get-Cluster -Name CPTHI05 | Get-VM -Name Vase*| Get-NetworkAdapter)){
    switch -regex ($nic.Name){
    '.+[1|2|3]'{Set-NetworkAdapter -NetworkAdapter $nic -Connected:$false -StartConnected:$false -Confirm:$false}
    '.+[4|5|6]'{Set-NetworkAdapter -NetworkAdapter $nic -Connected:$true -StartConnected:$true -Confirm:$false}
    }
}

Disconnect-VIServer 10.0.0.1 -Confirm:$false

Primary Site Recovery Manager SRM service account is :- JNJ\NAsupport

and Recovery Site Recovery Manager SRM service account is :- JNJ\MPsupport

Issue is:-

I have created the recover step in the Site Recovery manager recovery plan as below:-
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -file C:\Vaseline\vase.ps1

(If I loginto the PowerCLI console or command prompt manually with my domain account and run the script it will work fine... the same script if it runs from SRM Recovery Plan TEST or RUN this command will give below error:-

Once the SRM Test get complete I get below error under the SRM TEST History Report:-
Success: "File C:\Vaseline\vase.ps1 cannot be loaded because the execution of scripts is
disabled on this system. Please see "get-help about_signing" for more details.
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordE
xception
+ FullyQualifiedErrorId : RuntimeException

"

I need help on how to configure my powercli script mentioned above to run with above mentioned SRM service accounts.

Thank You

I appreciate LUCD for my previous PowerCLI questions.

0 Kudos
20 Replies
LucD
Leadership
Leadership

This might be a solution, it's based on a trick to bypass the ExecutionPolicy.

You can feed PowerShell script lines form standard input.

The line you would pass to SRM would look something like this

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noprofile -command "Get-Content -Path C:\Vaseline\vase.ps1 | C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noprofile -" 

The command reads your script and places it line by line in the pipeline.

Then you start another invocation of PowerShell, without executing the profile scripts.

This new PS engine will execute the script lines it finds in the pipeline.

The minus sign at the end is important, it tells PowerShell to read from the standard input.

To make sure you have the line, a copy in plain text

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noprofile -command "Get-Content -Path C:\Vaseline\vase.ps1 | C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noprofile -"

Again, this is one line.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos