Link to home
Create AccountLog in
Windows OS

Windows OS

--

Questions

--

Followers

Top Experts

Avatar of Tessando
Tessando🇺🇸

Getting Error "Cannot find drive. A drive with the name 'IIS' does not exist." When Using "Set-ItemProperty" in Powershell

I am attempting to access IIS using Powershell in order to automate installing a TLS/SSL Certificate on a Site.

In Powershell 7.2 I am using "Set-ItemProperty", passing a variable of the site and getting the following Error:

"Cannot find drive. A drive with the name 'IIS' does not exist."

Google-Fu is telling me that I need to import a WebAdministration Module, which I did at the beginning of the script. I've attempt three ways to import it, none of which are working:

Attempt 1:
Import-Module ServerManager
Add-WindowsFeature Web-Scripting-Tools

Attempt 2: 
Import-Module -Name 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration\WebAdministration.psd1'

Attempt 3:
Import-Module -Name 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration\WebAdministration.psd1' -UseWindowsPowerShell

Below is a screenshot of the roles installed on this Windows Server 2019 instance. According to MicroSoft these are this is the Feature that Powershell is using. 

How can I get "Set-ItemProperty" to find IIS? 

I'm so close and I appreciate the Sanity Check.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of David Johnson, CDDavid Johnson, CD🇨🇦

try in powershell 5 vice the open source powershell 7

Avatar of TessandoTessando🇺🇸

ASKER

Thank you, David. I'm using the cmdlet "Get-PfxCertificate", which I found out by trial-and-error doesn't work in the native Powershell 5. That is why I'm using Powershel. 7.2.

When I do run this in Powershell 5 I get a handful of errors, mostly Invalid Arguments. 

windows powershell 5.1
PS H:\Documents\WindowsPowerShell\Scripts> Get-PfxCertificate -FilePath 'H:\Documents\windows11 keys.pfx'
Enter password: ******


Thumbprint                                Subject
----------                                -------
FEE37E5A48FB927FC9EC42725A04221B029888EB  CN=DavidWayneJohnsonCD
PS H:\Documents\WindowsPowerShell\Scripts> Get-Host | Select-Object Version

Version
-------
5.1.25145.1000

Open in new window

Powershell 7
PS H:\Documents\WindowsPowerShell\Scripts> Get-Host | Select-Object Version

Version
-------
7.2.4

PS H:\Documents\WindowsPowerShell\Scripts> Get-PfxCertificate -FilePath 'H:\Documents\windows11 keys.pfx'
Enter password: ******
Thumbprint                                Subject              EnhancedKeyUsageList
----------                                -------              --------------------
FEE37E5A48FB927FC9EC42725A04221B029888EB  CN=DavidWayneJohnso… Encrypting File System

PS H:\Docum

Open in new window


Both work for me

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of TessandoTessando🇺🇸

ASKER

Okay, great. This does work for me in Powershell 5 if I pause to paste in the Password (which kills off the automation aspect of it).

That said, I'm still getting the same original error of "Cannot find drive. A drive with the name 'IIS' does not exist."

I'm totally fine solving this in Powershell 5.

are you doing this on the server that has IIS installed?
import-module WebAdministration
dir iis:\AppPools 

Open in new window

This should return something?

Avatar of TessandoTessando🇺🇸

ASKER

Ah, yes. So, it's turning into a bit of chicken-or-egg. In fact, I get a different set of responses between the native Powershell 5 and the newly installed Powershell 7.2. I gave it the same command as you did and I'm getting different results. On the left (with the black background) is Powershell 7.2, which gives the error I mentioned this AM. On the right is the native Powershell 5 which shoes the App Pools. Both are being ran "As Administrator".

I chose to go with Powershell 7.2 in order to pass the Password Credentials. Even when I paste in the Password I'm not getting the results I want, so this might be going back to the drawing board.

You don't have to answer this question (I can post a new one), but I'm really trying to replicate the October 2020 entry in this Let's Encrypt post. If you happen to have Powershell or a batch file that updates the TLS Certificate on an FTP site, I would love to take a look at it.

Thank you for your help. I appreciate the sanity check! :-)

User generated image



Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


passwords are usually encrypted

read-host -assecurestring | convertfrom-securestring | out-file C:\cred.txt
$password = cat c:\cred.txt | convertto-securestring

now you can use $password for -password

You mean this
Import-Module WebAdministration
$siteName = 'WEBftp'
$configItem = 'ftpServer.security.ssl.serverCertHash'
$thumb = '{new cert thumbprint}'
Set-ItemProperty "IIS:\Sites\$siteName" -Name $configItem -Value $thumb

Open in new window


lots of missing stuff there i.e the thumbprint  and what is $configItem

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


The article you linked to is from September 2020, not October 2020? typo?

$CertificatePath = 'H:\documents\windows11 keys.pfx'
$sSecStrPassword = ConvertTo-SecureString "yabbadabbad00" -AsPlainText -force
$certificateObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($CertificatePath, $sSecStrPassword)
$certificateObject.Thumbprint

Open in new window


Avatar of TessandoTessando🇺🇸

ASKER

Yes, you got it. So I was putting together this script to (1) grab the thumbprint, since that what is different, sans the name, and then (2) inserting that into the FTP site in IIS. Thanks to your help (and sticking to Powershell 5) I was able to get this to complete without errors.

What I expected to happen was to have the current SSL be replaced with the new one.

Instead, the old SSL got removed and nothing was in it's place (referring to when I look in IIS). Here's the script so far:

#Pre-Step Import the Modules in order to access IIS
Import-Module ServerManager
Add-WindowsFeature Web-Scripting-Tools

#Step 1: Let's get the Thumbprint of the Certificate & create the variable for the script
$cert = Get-PfxCertificate -Filepath "D:\CentralSSL\name-of-ftp-site.pfx" 
#-Password ($pwd = ConvertTo-SecureString -String "thisiswherethepasswordwouldgobutitsnotworkingAndThatsWhyItsCommentedOut" -Force -AsPlainText)
$cert.Thumbprint

#Step 2: Now let's replace the Certificate in IIS for our FTP site
Import-Module WebAdministration
$siteName = 'name-of-ftp-site'
$configItem = 'ftpServer.security.ssl.serverCertHash'
$thumb = $cert.Thumbprint
Set-ItemProperty "IIS:\Sites\$siteName" -Name $configItem -Value $thumb

#End 

Open in new window


A side note (and why it's commented out) is that I have to paste in the Password. This is why I originally went with Powershell 7.2, which allowed me to pass the password as plain text. (I have yet to attempt your suggestion, which I will do on Monday). 

The two roadblocks for me are (1) having to enter in the password and (2) having no TLS/SSL Certificate show up in IIS. Is there anything obvious I'm doing wrong?

Thanks for your help.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CDDavid Johnson, CD🇨🇦

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account
Windows OS

Windows OS

--

Questions

--

Followers

Top Experts

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.