VMware Cloud Community
Ranger_rkm
Contributor
Contributor
Jump to solution

Connecting to VIServer using Powershell within the same Powershell Script

Hello,

I'm current trying to learn how to work with Powershell and VMware ESX 3.5.

Here is what I'm doing. The second line is the one that is not working. I drap and drop this into the VI Enviroment Powershell prompt

If I manually type Connect-VIServer 72.19.25.5, I can get connected with no problem. So how do you do this from within the Powershell script?

connect-viserver -server 72.19.25.5 -user svcuser -password password

Import-Csv -Header Name "VM.csv" | Foreach { Get-VM -Name $_.Name | Shutdown-VMGuest -Confirm:$false }

Thanks for your help!

Mike

0 Kudos
1 Solution

Accepted Solutions
jeveenj
Enthusiast
Enthusiast
Jump to solution

You can try below step also:

Firstly you need to store credential:

New-VICredentialStoreItem -Host 192.168.101.1 -Password password -User root -file C:\vicredentials.xml

This will save the password in encrypted form in an xml.

Then perform below steps:

Step1. Creating a ps1 file. For example

$creds = Get-VICredentialStoreItem -file C:\vicredentials.xml
Connect-VIServer -Server $creds.Host -user $creds.User -password $creds.Password
Import-Csv "C:\VMReportTest1.csv" | foreach {
    Get-VM -Name $_.VMName | Shutdown-VMGuest -Confirm:$false}

Save this file as Connect.ps1 (change the name of file as per your requirement)

Step2. Write below line in notepad:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -command ".'C:\Documents and Settings\jeveenj\Desktop\ Connect.ps1'"

Save this file as RunScript.bat (change the name of file as per your requirement)

Please note:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe

Path for Windows PowerShell executable file

-PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1"

Path for Windows PowerShell console file

“ C:\Documents and Settings\jeveenj\Desktop\ Connect.ps1”

Shows the path of the ps1 file change it as per your requirement

Step3. Execute RunScript.bat.

Hope this helps

-If you found this information useful, please consider awarding points for Correct or Helpful.

View solution in original post

0 Kudos
6 Replies
jeveenj
Enthusiast
Enthusiast
Jump to solution

Hi Mike,

You can try below code snippet,

Connect-VIServer <server> -User root -Password password
Import-Csv "C:\VMName.csv" | foreach {Get-VM -Name $_.Name | Shutdown-VMGuest -Confirm:$false}

In this

1)"C:\VMName.csv" is the path of csv file

2)"$_.Name" takes the Name field of csv file, change it as per your requirement



-If you found this information useful, please consider awarding points for Correct or Helpful.

-If you found this information useful, please consider awarding points for Correct or Helpful.
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is it the Connect-VIServer that is not working or the Import-Csv ?

Could you perhaps tell us how you start the script and include the error messages your script is getting ?

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Ranger_rkm
Contributor
Contributor
Jump to solution

Hello,

Both of these are working fine. Its the running that is causing me the troubles. I'm trying to run both lines from a batch file. I did get it working but had to edit my PS profile with VIServer, User, Password. I did not want to have to do this, so I added the ConnectVIServer to the first line of code, but what happens is that the batch file will start up the PS VMware Enviroment fine, but never connect to VI Server.

I also was trying to do this in VBscript but had no luck. So with the current batch file, everything is working, but for security purposes I'd like to remove the PS peofile information and have it within a batch file or VBscript the I can compile to hide this information.

Thanks for both of your help,

Mike

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can use Hal's scripts to export and import credentials.

For detailed instructions on how to use this have a look at Import-PSCredential / Export-PSCredential

The way you call a PowerCLI script from a batch file can be found in Alan's post Running a PowerCLI Scheduled task. I know the post talks about scheduled tasks but the both methods described are equally valid for a BAT file.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
jeveenj
Enthusiast
Enthusiast
Jump to solution

You can try below step also:

Firstly you need to store credential:

New-VICredentialStoreItem -Host 192.168.101.1 -Password password -User root -file C:\vicredentials.xml

This will save the password in encrypted form in an xml.

Then perform below steps:

Step1. Creating a ps1 file. For example

$creds = Get-VICredentialStoreItem -file C:\vicredentials.xml
Connect-VIServer -Server $creds.Host -user $creds.User -password $creds.Password
Import-Csv "C:\VMReportTest1.csv" | foreach {
    Get-VM -Name $_.VMName | Shutdown-VMGuest -Confirm:$false}

Save this file as Connect.ps1 (change the name of file as per your requirement)

Step2. Write below line in notepad:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -command ".'C:\Documents and Settings\jeveenj\Desktop\ Connect.ps1'"

Save this file as RunScript.bat (change the name of file as per your requirement)

Please note:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe

Path for Windows PowerShell executable file

-PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1"

Path for Windows PowerShell console file

“ C:\Documents and Settings\jeveenj\Desktop\ Connect.ps1”

Shows the path of the ps1 file change it as per your requirement

Step3. Execute RunScript.bat.

Hope this helps

-If you found this information useful, please consider awarding points for Correct or Helpful.
0 Kudos
Ranger_rkm
Contributor
Contributor
Jump to solution

Thanks jeveenj, it worked great. I will use this a my master copy. Also thanks to everyone else that help me.

Take Care,

Mike

0 Kudos