Powerchell block is executing twice in Chef run and also machine is rebooting twice


I am trying change virtual memory path (pagefile.sys) for temporary storage drive in windows machine , then machine needs to restart post changing “pagefile.sys”.
After the change of this, also i m changing drive letter to next alphabet letter.
In powershell resource block i have added “not_if” condition to check the value as true/false. But still its getting executed twice in chef run.

Please find below code snippet and help me on this.

		powershell 'change the virtual memory path' do
			code <<-EOH
			$d_drive_pagefile_settings = Get-WmiObject Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ D:'"
			if (-not ([string]::IsNullOrEmpty($d_drive_pagefile_settings)))
			Import-Module ("#{Chef::Config[:file_cache_path]}/#{manage_virtualmemory_source_script}")
			Set-OSCVirtualMemory -SystemManagedSize C:
			Set-OSCVirtualMemory -None -DriveLetter D:
			Write-Host "d drive has page file settings"
			not_if <<-EOH
			([string]::IsNullOrEmpty((Get-WmiObject Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ D:'")))
			notifies :request, 'windows_reboot[restart_post_virtual_mem_update]', :immediately 

		windows_reboot 'restart_post_virtual_mem_update' do
			timeout 1
			reason 'Restarting the machine for changing virtual memory path'
			action :nothing

		powershell 'change the drive letter' do
			code <<-EOH
				$drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'D:'"
				Set-WmiInstance -input $drive -Arguments @{DriveLetter="F:";}
			not_if {reboot_pending?}


You can wrap your blocks of code in ``` to better format them and make them more readable. I edited your post as a moderator and did this for you.

For example:
my code here

In this case the backtick character is just being displayed because I’m escaping it with a backslash.

I’d start debugging this by running chef-client with -l debug and look for where the not_if is being executed and the resource is being run but you don’t expect it to be. You’ll see debugging out put showing the command being run.

One thought is that you’re wrapping Get-WmiObject with ([string]::IsNullOrEmpty(())) That could return false positives. You could try flipping that around to an only_if so other failures or issues wouldn’t trigger the block.