Hello,
I am trying to use start-job to make mass vm operations shorter.
When I run the test code below, I get "WARNING: The process cannot access the file 'C:\Users\user\AppData\Roaming\VMware\PowerCect]LI\RecentServerList.xml' because it is being used by another process." intermittently.
How should I work around?
Thank you,
Function Start-PowerCLIJob {
param(
[parameter(mandatory=$True)]
[VMware.VimAutomation.ViCore.Impl.V1.VIServerImpl]
$DefaultVIServer,
[string]
$JobName,
[parameter(mandatory=$True)]
[scriptblock]
$ScriptBlock,
[object[]]
$ArgumentList,
[psobject]
$InputObject,
[string[]]
$Modules = "VMware.VimAutomation.Core"
)
$ScriptBlockPrepend = {
Set-PowerCLIConfiguration -DisplayDeprecationWarnings:$false -Scope Session -confirm:$False | out-null;
$connection = Connect-ViServer -Server $Using:DefaultVIServer.name -session $Using:DefaultVIServer.SessionSecret;
}
$ScriptBlockPostpend = {
Disconnect-VIServer $connection -Confirm:$false -Force:$true
}
$ScriptBlock = [ScriptBlock]::Create($ScriptBlockPrepend.ToString() + $ScriptBlock.ToString()) # + $ScriptBlockPostpend.ToString())
$params = @{scriptblock=$ScriptBlock}
if ($JobName) {$params.Add('name',$JobName)}
if ($ArgumentList) {$params.Add('ArgumentList',$ArgumentList)}
if ($InputObject) {$params.Add('InputObject',$InputObject)}
Start-Job @params
}
#Start-PowerCLIJob -DefaultVIServer $DefaultVIServer -JobName "Do some stuff" -Module "VMware.VimAutomation.Core","VMware.DeployAutomation" -ScriptBlock {Do Stuff}
$vmlist = get-vm
foreach($eachvm in $vmlist) {
Start-PowerCLIJob -DefaultVIServer $DefaultVIServer -JobName "$($eachvm.Name)" -InputObject $eachvm.Name -ScriptBlock {
$data = $input
get-vm $data
}
}
Unfortunately, that is a known issue with PowerCLI (not being 100% thread-safe).
I normally package my Connect-VIServer line in the background job code in a loop.
Just keep retrying until the connection in the background job works.
A better solution might be to check the lock on said file, but I have seen cases where the lock was taken just after the test.
So I deemed that option not 100% fool-proof (and reverted to the try-wait-retry loop)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LucD for quick reply. I will keep that in mind when I write parallel codes.
And can it be like this?
while($connection -ne "NameOfvCenter"){
$connection = Connect-ViServer -Server $Using:DefaultVIServer.name -session $Using:DefaultVIServer.SessionSecret;
}
I misread the message earlier.
This is on the Connect-VIServer cmdlet, that one is only a Warning.
You can ignore it, it just says that the counter in the recentservlist.xml for that server can not be updated.
I had a problem with the Set-PowerCLIConfiguration cmdlet.
That one writes to one or both of the PowerCLI_Settings.xml files (depending on the Scope).
When that one fails due to a file lock, it is an error.
Btw, the $global:DefaultViServer has a handy property IsConnected that is ideal for testing if the connection succeeded.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LucD for more information.
Oh I see,
So I can ignore recentserverlist.xml error and still connects to vcenter, right?
How do you prevent powercli_settings.xml error?
I don't want to have that error in actual process.
Is there also some kind of property to check in while statement?
In the test environment with 5VMs, the following code ran fine. My goal is to change scsiadapter from lsilogic to vmware paravirtual on 500 VDIs.
Thank you,
$VCenterName = "vcenterFQDN"
Function Start-PowerCLIJob {
param(
[parameter(mandatory=$True)]
[VMware.VimAutomation.ViCore.Impl.V1.VIServerImpl]
$DefaultVIServer,
[string]
$JobName,
[parameter(mandatory=$True)]
[scriptblock]
$ScriptBlock,
[object[]]
$ArgumentList,
[psobject]
$InputObject,
[string[]]
$Modules = "VMware.VimAutomation.Core"
)
$ScriptBlockPrepend = {
Set-PowerCLIConfiguration -DisplayDeprecationWarnings:$false -Scope Session -confirm:$False | out-null;
while($connection.name -ne $using:VCenterNAme){
$connection = Connect-ViServer -Server $Using:DefaultVIServer.name -session $Using:DefaultVIServer.SessionSecret;
}
}
$ScriptBlock = [ScriptBlock]::Create($ScriptBlockPrepend.ToString() + $ScriptBlock.ToString() )#
#$ScriptBlock = [ScriptBlock]::Create($ScriptBlock.ToString())
$params = @{scriptblock=$ScriptBlock}
if ($JobName) {$params.Add('name',$JobName)}
if ($ArgumentList) {$params.Add('ArgumentList',$ArgumentList)}
if ($InputObject) {$params.Add('InputObject',$InputObject)}
Start-Job @params
}
#Start-PowerCLIJob -DefaultVIServer $DefaultVIServer -JobName "Do some stuff" -Module "VMware.VimAutomation.Core","VMware.DeployAutomation" -ScriptBlock {Do Stuff}
$vmlist = get-vm | Where{$_.Name -like "VDI*"}
foreach($eachvm in $vmlist) {
Start-PowerCLIJob -DefaultVIServer $DefaultVIServer -JobName "$($eachvm.Name)" -InputObject $eachvm.Name -ScriptBlock {
$vm = get-vm $input
$vm | Shutdown-VMGuest -confirm:$false
while((get-vm $vm).PowerState -ne "PoweredOff"){
Start-Sleep -Seconds 2
Write-Host waiting 1st shutdown for $vm.Name ...
}
$vm | New-HardDisk -CapacityGB 1 -ThinProvisioned:$true |New-ScsiController -type ParaVirtual
$vm | start-vm | Wait-Tools
start-sleep -seconds 60
$vm | Shutdown-VMGuest -confirm:$false
while((get-vm $vm).PowerState -ne "PoweredOff"){
Start-Sleep -Seconds 2
Write-Host waiting 2nd shutdown for $vm.Name ...
}
$vm | Get-ScsiController | Where-Object{$_.Type -eq "ParaVirtual"}
$vm | Get-HardDisk |Where-object{$_.CapacityGB -eq 1} | Remove-HardDisk -DeletePermanently:$true -Confirm:$false
$vm | Get-ScsiController | Where-object{$_.Type -eq "VirtualLsiLogicSAS"} | Set-ScsiController -Type ParaVirtual
#$vm | Get-ScsiController | Set-ScsiController -Type VirtualLsiLogicSAS
write-host waiting for reboot for $vm.Name .....
$vm | Start-VM | Wait-Tools
Write-Host done!
}
}
The most straightforward way to avoid this warning, is to use a delay between the Start-Job invocations.
On the calling side do a Get-Job for your last started job and wait till it says 'running'.
Then add a short delay (how much depends on your platform and the script you are running).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I will try.
Thank you!!
Hello I am also facing the locking issue for powercli_settings.xml, when 2 processes are using powerCLI cmdlets.
How can we avoid it?
You can't.
If you don't care about the most recently connected servers, ignore it.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Is there any way to hide this message? I am using the new threading capability via "Foreach-Object -Parallel" to run large scale migrations with several tasks running at a time. I've added a random sleep before each attempt which helps a little but still regularly sprays yellow text all over the console.
Start-Sleep -Milliseconds (Get-Random -Minimum 500 -Maximum 5000)
$sourcevCenterConnection = Connect-VIServer -Server $sourcevCenter -Credential $vCenterCreds -Force -ErrorAction SilentlyContinue
No
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can add "-WarningAction SilentlyContinue" to the "connect-viserver....." and you're all set. Works for me. I am using PowerCLI v13.1 if that's different from other versions.