Hi,
I'm not working on hiding my credentials using an XML file instead of having the creds directly in the script.
When I call the creds using an XML file it not works inside of powercli, but when I use plaintext directly in my ps1 file,it works.
What am I missing??
I'm use the follow command to create the xml file:
New-VICredentialStoreItem -Host '192.168.0.99' -User 'administrator@vsphere.local' -Password 'password' -File D:\Scripts\myvc.xml
ARSnap.ps1 contents:
#add vmware module command
Add-PSSnapin -Name Vmware*
#connect info
$vcenterip = '192.168.0.99'
$username = 'administrator@vsphere.local'
#save encrypted xml file
$Creds = Get-VICredentialStoreItem -Host $vcenterip -User $username -File D:\Scripts\myvc.xml
#format datetime
$Date = Get-Date -UFormat %Y%m%d
$timechop = Get-Date -Format g
$weekday = Get-Date -UFormat %A
#connect to vc
Connect-viserver $Creds.Host -User $Creds.User -Password $Creds.Password
#create snapshot
get-vm | where {$_.PowerState -eq "PoweredOn"} | where {$_.Name -match "Linux"} | New-Snapshot -Name "$Date" -Memory:$true -Description "PowerCLI Weekly Snapshot at $timechop $weekday"
#disconnect vc
Disconnect-VIServer -Confirm:$false
here are my settings in the task scheduler:
Windows Server 2008 R2
General Tab:
An Local Admin Runs the Task
Run whether user is logged on or not
Run with highest privilages
Trigger Tab:
Weekly Friday at 7:00
Actions Tab:
Program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Argument: -ExecutionPolicy Bypass -NonInteractive -NoLogo -NoProfile -Command "& 'D:\Scripts\ARSnap.ps1'"
Path:D:\Scripts
and my env:
PS C:\Windows\system32> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.14409.1005
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1005
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Get-Module -Name VMware* -ListAvailable
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 6.7.0.1... VMware.DeployAutomation {Add-DeployRule, Add-ProxyServer, Add-ScriptBundle, Copy-DeployRule...}
Script 6.7.0.1... VMware.ImageBuilder {Add-EsxSoftwareDepot, Add-EsxSoftwarePackage, Compare-EsxImageProfile, Exp...
Manifest 11.1.0.... VMware.PowerCLI
Script 6.7.0.1... VMware.Vim
Script 11.0.0.... VMware.VimAutomation.Cis.Core {Connect-CisServer, Disconnect-CisServer, Get-CisService}
Script 11.0.0.... VMware.VimAutomation.Cloud {Add-CIDatastore, Connect-CIServer, Disconnect-CIServer, Get-Catalog...}
Script 11.0.0.... VMware.VimAutomation.Common
Script 11.0.0.... VMware.VimAutomation.Core {Add-PassthroughDevice, Add-VirtualSwitchPhysicalNetworkAdapter, Add-VMHost...
Script 7.6.0.1... VMware.VimAutomation.HorizonView {Connect-HVServer, Disconnect-HVServer}
Script 10.0.0.... VMware.VimAutomation.License Get-LicenseDataManager
Script 11.0.0.... VMware.VimAutomation.Nsxt {Connect-NsxtServer, Disconnect-NsxtServer, Get-NsxtService}
Script 11.0.0.... VMware.VimAutomation.Sdk {Get-ErrorReport, Get-PSVersion, Get-InstallPath}
Script 11.0.0.... VMware.VimAutomation.Security {Get-SecurityInfo, Get-VTpm, Get-VTpmCertificate, Get-VTpmCSR...}
Script 11.1.0.... VMware.VimAutomation.Srm {Connect-SrmServer, Disconnect-SrmServer}
Script 11.1.0.... VMware.VimAutomation.Storage {Add-KeyManagementServer, Copy-VDisk, Export-SpbmStoragePolicy, Get-KeyMana...
Script 1.3.0.0 VMware.VimAutomation.StorageUtility Update-VmfsDatastore
Script 11.0.0.... VMware.VimAutomation.Vds {Add-VDSwitchPhysicalNetworkAdapter, Add-VDSwitchVMHost, Export-VDPortGroup...
Script 11.0.0.... VMware.VimAutomation.Vmc {Connect-Vmc, Disconnect-Vmc, Get-VmcSddcNetworkService, Get-VmcService...}
Script 10.0.0.... VMware.VimAutomation.vROps {Connect-OMServer, Disconnect-OMServer, Get-OMAlert, Get-OMAlertDefinition...}
Script 6.5.1.7... VMware.VumAutomation {Add-EntityBaseline, Copy-Patch, Get-Baseline, Get-Compliance...}
many thanks.
Try adding this at the beginning of your script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The VICredentialStoreItem is linked to the user and computer on which you created it.
Is the scheduled task running under the same account with which you created the credential store item?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, I'm running the scheduled task with same account.
BTW:
When I Under the PowerShell command line, use copy & paste echo command and execute from my ps1 file, It work fine.
in both way (call xml file or use plaintext) windows scheduled task running no errors,and return 0.
but when I use xml file,my snapshot wasn't create success, use plaintext ps1 file snapshot was create success.
so,what's wrong with this?
What do you have set up as PowerCLI configuration for the account?
Do a Get-PowerCLIConfiguration.
Is InvalidCertificateAction set to Ignore, is DisplayDeprecationWarnings set to $false?
For all scopes?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
PS C:\Windows\system32> Get-PowerCLIConfiguration
Scope ProxyPolicy DefaultVIServerMode InvalidCertificateAction DisplayDeprecationWarnings WebOperationTimeout
Seconds
----- ----------- ------------------- ------------------------ -------------------------- -------------------
Session NoProxy Multiple Ignore True 300
User NoProxy Ignore
AllUsers
PS C:\Windows\system32> Get-PowerCLIConfiguration -Scope AllUsers
Scope ProxyPolicy DefaultVIServerMode InvalidCertificateAction DisplayDeprecationWarnings WebOperationTimeout
Seconds
----- ----------- ------------------- ------------------------ -------------------------- -------------------
AllUsers
PS C:\Windows\system32> Get-PowerCLIConfiguration -Scope User
Scope ProxyPolicy DefaultVIServerMode InvalidCertificateAction DisplayDeprecationWarnings WebOperationTimeout
Seconds
----- ----------- ------------------- ------------------------ -------------------------- -------------------
User NoProxy Ignore
PS C:\Windows\system32> Get-PowerCLIConfiguration -Scope Session
Scope ProxyPolicy DefaultVIServerMode InvalidCertificateAction DisplayDeprecationWarnings WebOperationTimeout
Seconds
----- ----------- ------------------- ------------------------ -------------------------- -------------------
Session NoProxy Multiple Ignore True 300
which parameter need to change?
Try adding this at the beginning of your script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have been tested for your suggestion and it worked.
thank you!