Set-ItemProperty working in PowerShell 5.1, but not in 7.0

Viewed 463

I installed PowerShell 7.0 and started some testing. The following script works with no errors in PowerShell 5.1.

Import-Module WebAdministration

Set-ItemProperty "IIS:\Sites\TestSite" -Name physicalPath -Value "D:\Dev\Test\Website\WebSiteDown"

In PowerShell 7.0 I tried to change the import to use the updated "IISAdministration" module instead of the older "WebAdministration". I get the same error with both modules.

Set-ItemProperty: D:\Dev\OProTrunk\Website\WebSiteDown\OctopusCommand\Test.ps1:3
Line |
   3 |  Set-ItemProperty "IIS:\Sites\TestSite" -Name physicalPath -Value  …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot find drive. A drive with the name 'IIS' does not exist.

Both were running as administrator to be able to update the web site physical path. Do I need to format the property different with PowerShell 7.0?

1 Answers

As far as I can tell, you will need to interact with your compatibility session for Windows PowerShell here.

Import-Module WebAdministration
$session = Get-PSSession -Name WinPSCompatSession
$sb = {Set-ItemProperty "IIS:\Sites\TestSite" -Name physicalPath -Value "D:\Dev\Test\Website\WebSiteDown"}
Invoke-Command -Scriptblock $sb -Session $session

The IIS PSDrive is created by the WebAdministration provider. Since that provider only loads in Windows PowerShell (at least on my system), you will need to access it there.

Related