PS script help

94 views
Skip to first unread message

Dave Lum

unread,
Jul 2, 2026, 11:26:07 AMJul 2
to ntsys...@googlegroups.com

I am looking to use PowerShell to change IIS pool identities.  I have 20 app pools and I’d like the $appPoolName part be able to cycle thorough values in a TXT file so the credentials get applied to whatever app pool names I have in the TXT file.  The Application Pool names aren’t consistent enough for me to just use a filter on the name so a TXT file is the more reliable route here.

$appPoolName = "AppPool_1"

$username = "DOMAIN\ACCOUNT$"

 

# Apply the custom identity credentials

Set-ItemProperty "IIS:\AppPools\$appPoolName" -Name processModel -Value @{

    identityType = 3;

    userName = $username;

  

}

 

TIA,

Dave Lum (he/him)

Systems Administrator
Work hours: Tues – Fri 5:30a – 4:30p Pacific
P:
503.546.2163
E: lu...@ochin.org

Facebook LinkTwitter LinkLinkedin Link www.ochin.org
OCHIN email

 

Attention: Information contained in this message and or attachments is intended only for the recipient(s) named above and may contain confidential and or privileged material that is protected under State or Federal law. If you are not the intended recipient, any disclosure, copying, distribution or action taken on it is prohibited. If you believe you have received this email in error, please contact the sender with a copy to compl...@ochin.org, delete this email and destroy all copies.

Solodow, Damien

unread,
Jul 2, 2026, 11:31:42 AMJul 2
to ntsys...@googlegroups.com

Are you planning on using the same username for all of the app pools you want to change?

 

Gaylor Electric logo

Facebook logo

Instagram logo

LinkedIn logo

X logo

YouTube logo

Damien Solodow
IT Engineering Manager

Gaylor Electric, Inc.
10405 Crosspoint Blvd
Indianapolis, IN. 46256
O: 317.815.3103 | M: 317.506.8521

--
You received this message because you are subscribed to the Google Groups "ntsysadmin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntsysadmin+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/ntsysadmin/DM8PR17MB4918A37B41DD24FB160FAA72DDF52%40DM8PR17MB4918.namprd17.prod.outlook.com.

Michael B. Smith

unread,
Jul 2, 2026, 11:35:49 AMJul 2
to ntsys...@googlegroups.com

You know that “Get-ChildItem IIS:\AppPools” returns all appPools (including their name)?

 

(I understand that you might still just want to update certain appPools – just askin’)

 

From: 'Dave Lum' via ntsysadmin <ntsys...@googlegroups.com>
Sent: Thursday, July 2, 2026 11:26 AM
To: 'ntsys...@googlegroups.com' <ntsys...@googlegroups.com>
Subject: [ntsysadmin] PS script help

 

I am looking to use PowerShell to change IIS pool identities.  I have 20 app pools and I’d like the $appPoolName part be able to cycle thorough values in a TXT file so the credentials get applied to whatever app pool names I have in the TXT file.  The Application Pool names aren’t consistent enough for me to just use a filter on the name so a TXT file is the more reliable route here.

$appPoolName = "AppPool_1"

--

Dave Lum

unread,
Jul 2, 2026, 11:58:39 AMJul 2
to ntsys...@googlegroups.com

Yes

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Solodow, Damien
Sent: Thursday, July 2, 2026 8:32 AM
To: ntsys...@googlegroups.com
Subject: [ntsysadmin] RE: PS script help

 

CAUTION: This email originated from outside of OCHIN’s network

Do not click links or open attachments unless you recognize the sender and know the content is safe. If you suspect this email is phishing or a scam, use the report button in the Outlook toolbar to report it to Desktop Support.

 

Dave Lum

unread,
Jul 2, 2026, 11:59:26 AMJul 2
to ntsys...@googlegroups.com

Yep.  I do that, drop to a file and them remove the pools I don’t want to change.

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Michael B. Smith
Sent: Thursday, July 2, 2026 8:36 AM
To: ntsys...@googlegroups.com
Subject: [ntsysadmin] RE: PS script help

 

CAUTION: This email originated from outside of OCHIN’s network

Do not click links or open attachments unless you recognize the sender and know the content is safe. If you suspect this email is phishing or a scam, use the report button in the Outlook toolbar to report it to Desktop Support.

 

You know that “Get-ChildItem IIS:\AppPools” returns all appPools (including their name)?

Dave Lum

unread,
Jul 2, 2026, 12:03:34 PMJul 2
to ntsys...@googlegroups.com

This is what I have so far, but I get Cannot find path 'IIS:\AppPools\MyAppPool1 because it does not exist.  If I remove the foreach parts and run the script replacing the $inputs line with $appPoolName = "MyAppPool1", it works.


$inputs = Get-Content -Path "C:\Automate\Reports\IISAppPools.txt"

$username = "DOMAIN\ACCOUNT"

 

foreach ($appPoolName in $inputs) {

    # Process each line here

    Write-Output "Processing: $appPoolName"

      

 

Stop-WebAppPool -Name $appPoolName

 

Start-Sleep -Seconds 10

 

# Apply the custom identity credentials

Set-ItemProperty "IIS:\AppPools\$appPoolName" -Name processModel -Value @{

    identityType = 3;

    userName = $username;

  

}

 

Start-WebAppPool -Name $appPoolName

 

}

Dave

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Michael B. Smith
Sent: Thursday, July 2, 2026 8:36 AM
To: ntsys...@googlegroups.com
Subject: [ntsysadmin] RE: PS script help

 

CAUTION: This email originated from outside of OCHIN’s network

Do not click links or open attachments unless you recognize the sender and know the content is safe. If you suspect this email is phishing or a scam, use the report button in the Outlook toolbar to report it to Desktop Support.

 

You know that “Get-ChildItem IIS:\AppPools” returns all appPools (including their name)?

Solodow, Damien

unread,
Jul 2, 2026, 12:07:39 PMJul 2
to ntsys...@googlegroups.com

Does the write-output have the right app pool name, and does it successfully stop the app pool?

What I’d try is this change to the set-itemproperty line:

 

Set-ItemProperty "IIS:\AppPools\$($appPoolName)" -Name processModel -Value @{

 

 

Gaylor Electric logo

Facebook logo

Instagram logo

LinkedIn logo

X logo

YouTube logo

Damien Solodow
IT Engineering Manager

Gaylor Electric, Inc.
10405 Crosspoint Blvd
Indianapolis, IN. 46256
O: 317.815.3103 | M: 317.506.8521

 

Dave Lum

unread,
Jul 2, 2026, 12:27:36 PMJul 2
to ntsys...@googlegroups.com

In the script without foreach the correct app name is displayed and the app pool does stop. Wrapping foreach in there I get “object not found”.

My export command is Get-ChildItem -Path IIS:\AppPools\ | Select-Object name | Format-Table -AutoSize and it lists the app pools, and the TXT file has just the name property for each pool in it.

Dave

Wright, John M

unread,
Jul 2, 2026, 12:31:42 PMJul 2
to ntsys...@googlegroups.com

From what you’re saying, I would think there was something wrong with the string—except that it seems to be failing for you at Set-ItemProperty while succeeding at Stop-WebAppPool.  And if it works when you hard-code the variable, it can’t be what prepends it on the path for Set-ItemProperty.

 

So a wild guess.  What do you get from the following?

 

$content = Get-Content -Path "C:\Automate\Reports\IISAppPools.txt"

$content[0].gettype()

 

--

John Wright

IT Support Specialist

1800 Old Bluegrass Avenue, Louisville, KY 40215

502.708.9953

Please submit IT requests to Hazelwoo...@bluegrass.org

24 Hour Helpline 1.800.928.8000

  

CONFIDENTIALITY NOTICE: This message contains confidential information and is intended only for the individual(s) addressed in the message. If you are not the named addressee, you should not disseminate, distribute, or copy this e-mail. If you are not the intended recipient, you are notified that disclosing, distributing, or copying this e-mail is strictly prohibited.

 

From: 'Dave Lum' via ntsysadmin <ntsys...@googlegroups.com>
Sent: Thursday, July 2, 2026 12:03 PM
To: ntsys...@googlegroups.com
Subject: [ntsysadmin] RE: PS script help

 

EXTERNAL EMAIL - This email was sent by a person from outside your organization. Exercise caution when clicking links, opening attachments or taking further action, before validating its authenticity.

Secured by Check Point

Wright, John M

unread,
Jul 2, 2026, 12:34:38 PMJul 2
to ntsys...@googlegroups.com

I’m thinking it’s the data type:

 

(Get-ChildItem -Path IIS:\AppPools\ | Select-Object name | Format-Table -AutoSize)[0].gettype()

 

IsPublic IsSerial Name                                     BaseType

-------- -------- ----                                     --------

False    False    FormatStartData                          Microsoft.PowerShell.Commands.Internal.Format.StartData

 

 

Probably work if you export like:   (Get-ChildItem -Path IIS:\AppPools\ | Select-Object name | Format-Table -AutoSize).name

--

John Wright

IT Support Specialist

1800 Old Bluegrass Avenue, Louisville, KY 40215

502.708.9953

Please submit IT requests to Hazelwoo...@bluegrass.org

24 Hour Helpline 1.800.928.8000

  

CONFIDENTIALITY NOTICE: This message contains confidential information and is intended only for the individual(s) addressed in the message. If you are not the named addressee, you should not disseminate, distribute, or copy this e-mail. If you are not the intended recipient, you are notified that disclosing, distributing, or copying this e-mail is strictly prohibited.

 

From: 'Dave Lum' via ntsysadmin <ntsys...@googlegroups.com>
Sent: Thursday, July 2, 2026 12:27 PM
To: ntsys...@googlegroups.com
Subject: [ntsysadmin] RE: PS script help

 

EXTERNAL EMAIL - This email was sent by a person from outside your organization. Exercise caution when clicking links, opening attachments or taking further action, before validating its authenticity.

Secured by Check Point

 

In the script without foreach the correct app name is displayed and the app pool does stop. Wrapping foreach in there I get “object not found”.

Dave Lum

unread,
Jul 2, 2026, 12:35:25 PMJul 2
to ntsys...@googlegroups.com

PS C:\WINDOWS\system32> $content = Get-Content -Path "C:\Automate\Reports\IISAppPools.txt"

$content[0].gettype()

 

IsPublic IsSerial Name                                     BaseType                                                                      

-------- -------- ----                                     --------                                                                       

True     True     String                                   System.Object  

Mayo, Bill

unread,
Jul 14, 2026, 11:07:29 AMJul 14
to ntsys...@googlegroups.com

I am working on moving some automation from a proprietary method that generated PowerShell scripts to doing the same commands via a PowerShell module. I have been doing stuff with PowerShell modules for quite a while and am pretty comfortable with it. The PowerShell module contains the functions and lives in a directory in the PSPath and auto-importing works as expected. One of the main things I am tackling here is commands that have to run against either on-prem Exchange or Exchange Online. We are in hybrid mode, FWIW. I have 2 problems and would appreciate any insight/information that anyone could provide.

 

The first problem is related to modularizing various functions. It is possible that a script would call multiple functions that rely on an Exchange connection, so what I did was create a function that attempts to determine if there is currently an Exchange session or not. If there is, it will do nothing. If there is not, it will create one. In this particular instance, we are talking about an Import-Session that connects to on-prem Exchange server. If I run this code block from a PowerShell session, all commands are imported and things work correctly. If I manually copy/paste this function into a PS window and then call it, it works correctly. If I just call the function in the window or in a script (pulled from the module), the commands are not imported. The output from the commands is exactly the same and I even updated the function to run a Get-Command from the import session, and it shows the commands listed. However, if I try to run the command, it says it is not recognized. The extremely odd thing about this is that it was originally working and just stopped. So I guess the core question is, “Should Import-Session called inside a function make commands persistently available in the calling PS session or not?” If not, is there anything I can do here to make it work? Otherwise, I have a mess to deal with. I already know if you keep re-importing the session, that doesn’t work. Here is the function in case that helps in any way (note that I originally didn’t have AllowClobber and just added that today during additional testing, to no avail):

 

Function Connect-XXXExchangeOnPrem

{

    Write-Host "Checking Exchange Connection"

    $ExchangeSessions = Get-PsSession | Where-Object ComputerName -eq 'myserver.contoso.com'

    If ($Null -eq $ExchangeSessions) {

        Write-Host "Importing Exchange Session"

        $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://myserver.contoso.com/PowerShell/ -Authentication Kerberos

        $ImportSession = Import-PSSession $ExchangeSession -Verbose -AllowClobber

        Get-Command -Module $ImportSession

    } Else {

        Write-Host "Exchange Session Already Exists"

    }

}

 

The second problem is not currently as big of a deal. The issue is that anytime that I have to intermingle connections to both on-prem (as above) and online (connect-exchangeonline), commands will not work. The problem with this one seems more obvious in that there is overlap of the commands and this maybe cannot be worked around. I recognize that an option may be to target commands to online always, although I am not completely sure (still new to M365) that will work for everything. But it does seem like importing both in a session winds up with neither working. The kind of thing where I wind up needing this is generally in scripts that try to manipulate things for which there is no “remote” version of the command, like Add-MailboxFolderPermission.

 

Bill Mayo

Michael B. Smith

unread,
Jul 14, 2026, 11:21:06 AMJul 14
to ntsys...@googlegroups.com

Eh, the second problem is easier, but painful. You can fully qualify a cmdlet name. That is, specify it as modulename\cmdlet-name. Also, see -prefix for import-module.

 

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_command_precedence?view=powershell-5.1#use-module-qualified-names

 

The traditional way to do this is to re-implement the functions as proxy functions. That is, for Get-Mailbox, have a Get-Mailbox in your script that checks to see if you are connected to on-premises or Exchange Online and then call the appropriate fully qualified function.

 

The first one seems vaguely familiar, but I’m not 100% sure... Are you doing the import-session inside of a module function? Or is it part of the mainline script? I seem to remember that there are some issues with global scope conflicting with module variables.

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Mayo, Bill
Sent: Tuesday, July 14, 2026 11:07 AM
To: ntsys...@googlegroups.com

--

You received this message because you are subscribed to the Google Groups "ntsysadmin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntsysadmin+...@googlegroups.com.

Mayo, Bill

unread,
Jul 14, 2026, 11:40:05 AMJul 14
to ntsys...@googlegroups.com

Thanks for the info, that is something of which I was not particularly aware.

 

As for the question, yes, I am doing the import-session inside of a module function. That import-session function gets called by other functions in the same module.

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Michael B. Smith
Sent: Tuesday, July 14, 2026 11:21 AM
To: ntsys...@googlegroups.com

Subject: [ntsysadmin] RE: Import-Session behaving differently from module

 

EXTERNAL EMAIL: This email originated from outside of Pitt County Government. Do not click any links or open any attachments unless you trust the sender and know the content is safe.

Michael B. Smith

unread,
Jul 14, 2026, 11:46:41 AMJul 14
to ntsys...@googlegroups.com

On the first issue – there are known issues with polluting the global state from modules. And they’ve never been fixed because some people depend on the behavior.

 

I asked Grok and it gave details. If you don’t have Grok, you can try ChatGpt or Claude.

Wright, John M

unread,
Jul 14, 2026, 12:02:50 PMJul 14
to ntsys...@googlegroups.com

For the second problem, I don’t know of any workaround.  You can remove a module after it’s been imported, but that doesn’t unload the cmdlets.  In fact, I don’t think you can do that without destroying the whole session and restarting.

 

I’d be interested to know whether there’s really any need to address the on-prem Exchange server.  We also have a hybrid environment, and when I have to do mail-related things, I just target Exchange Online, and never the on-prem.

 

If you can tell me what you’d query the on-prem for, I might be able to tell you whether it could work for 365.

 

--

John Wright

IT Support Specialist

1800 Old Bluegrass Avenue, Louisville, KY 40215

502.708.9953

Please submit IT requests to Hazelwoo...@bluegrass.org

24 Hour Helpline 1.800.928.8000

  

CONFIDENTIALITY NOTICE: This message contains confidential information and is intended only for the individual(s) addressed in the message. If you are not the named addressee, you should not disseminate, distribute, or copy this e-mail. If you are not the intended recipient, you are notified that disclosing, distributing, or copying this e-mail is strictly prohibited.

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Mayo, Bill
Sent: Tuesday, July 14, 2026 11:07 AM
To: ntsys...@googlegroups.com
Subject: [ntsysadmin] Import-Session behaving differently from module

 

EXTERNAL EMAIL - This email was sent by a person from outside your organization. Exercise caution when clicking links, opening attachments or taking further action, before validating its authenticity.

Secured by Check Point

 

I am working on moving some automation from a proprietary method that generated PowerShell scripts to doing the same commands via a PowerShell module. I have been doing stuff with PowerShell modules for quite a while and am pretty comfortable with it. The PowerShell module contains the functions and lives in a directory in the PSPath and auto-importing works as expected. One of the main things I am tackling here is commands that have to run against either on-prem Exchange or Exchange Online. We are in hybrid mode, FWIW. I have 2 problems and would appreciate any insight/information that anyone could provide.

--

You received this message because you are subscribed to the Google Groups "ntsysadmin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntsysadmin+...@googlegroups.com.

Mayo, Bill

unread,
Jul 14, 2026, 12:16:05 PMJul 14
to ntsys...@googlegroups.com

A key thing would be for a new user, which is Enable-RemoteMailbox. That is a primary reason why I find myself in mixed mode trying to do connect to both. I recognize I can use Set-Mailbox targeted to EOL as opposed to Set-RemoteMailbox, but I find myself using the latter to be consistent with what I am doing in the new user function.

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Wright, John M
Sent: Tuesday, July 14, 2026 12:03 PM
To: ntsys...@googlegroups.com

Mayo, Bill

unread,
Jul 14, 2026, 12:21:21 PMJul 14
to ntsys...@googlegroups.com

Fortunately or unfortunately, I don’t  have any AI subscriptions. I will try to dig into it further, with the info you provided. As I mentioned in my original message, what is really throwing me is that this was working for weeks (first problem, not second) and has just stopped working. It started failing because my “is the remote session already connected” check started failing and it kept re-importing over and over. In the process of fixing that, I then started hitting this other problem.

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Michael B. Smith
Sent: Tuesday, July 14, 2026 11:47 AM
To: ntsys...@googlegroups.com
Subject: [ntsysadmin] RE: Import-Session behaving differently from module

 

On the first issue – there are known issues with polluting the global state from modules. And they’ve never been fixed because some people depend on the behavior.

 

I asked Grok and it gave details. If you don’t have Grok, you can try ChatGpt or Claude.

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Mayo, Bill
Sent: Tuesday, July 14, 2026 11:40 AM
To: ntsys...@googlegroups.com
Subject: [ntsysadmin] RE: Import-Session behaving differently from module

 

Thanks for the info, that is something of which I was not particularly aware.

 

As for the question, yes, I am doing the import-session inside of a module function. That import-session function gets called by other functions in the same module.

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Michael B. Smith
Sent: Tuesday, July 14, 2026 11:21 AM
To: ntsys...@googlegroups.com
Subject: [ntsysadmin] RE: Import-Session behaving differently from module

 

Eh, the second problem is easier, but painful. You can fully qualify a cmdlet name. That is, specify it as modulename\cmdlet-name. Also, see -prefix for import-module.

Wright, John M

unread,
Jul 14, 2026, 12:52:58 PMJul 14
to ntsys...@googlegroups.com

Hmm.  We do connect to on-prem when we create accounts, and we target enable-remotemailbox against our Exchange server.  But then we don’t use set-mailbox for anything.

 

Essentially, our user creation scripts (leaving out a lot of detail) do this:

 

  1. New-ADUser, running against on-prem DC.  All the attributes are supplied here.
  2. Enable-RemoteMailbox against on-prem Exchange.

 

I’m sorry if I was misleading before.  I was thinking about all the other mail-oriented scripts I use.   But in new user creation, it’s all against on-prem servers, which then sync within about 40-45 to 365.

 

--

John Wright

IT Support Specialist

1800 Old Bluegrass Avenue, Louisville, KY 40215

502.708.9953

Please submit IT requests to Hazelwoo...@bluegrass.org

24 Hour Helpline 1.800.928.8000

  

CONFIDENTIALITY NOTICE: This message contains confidential information and is intended only for the individual(s) addressed in the message. If you are not the named addressee, you should not disseminate, distribute, or copy this e-mail. If you are not the intended recipient, you are notified that disclosing, distributing, or copying this e-mail is strictly prohibited.

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of Mayo, Bill
Sent: Tuesday, July 14, 2026 12:16 PM
To: ntsys...@googlegroups.com
Subject: [ntsysadmin] RE: Import-Session behaving differently from module

 

EXTERNAL EMAIL - This email was sent by a person from outside your organization. Exercise caution when clicking links, opening attachments or taking further action, before validating its authenticity.

Secured by Check Point

Michael B. Smith

unread,
Jul 14, 2026, 1:00:46 PMJul 14
to ntsys...@googlegroups.com

Probably – SWAG – because you just hit the global state issue. Do you use git for your scripts? If you can identify the change, you can probably figure out why it happened.

Mayo, Bill

unread,
Jul 14, 2026, 2:57:34 PMJul 14
to ntsys...@googlegroups.com

What you are doing sounds logical, I think my issue is that the first script update I made was for new accounts and I was using that a reference and continued using the “x-RemoteMailbox” structure because I had already done that. I will need to go back through and make another pass on those to clean that up.

Mayo, Bill

unread,
Jul 14, 2026, 3:11:23 PMJul 14
to ntsys...@googlegroups.com

I have my own home-grown method for keeping track of changes, so I know what this function was before. It had been working for some time, and then last week I had a script completely blow up because it kept creating new sessions to Exchange. It was clear that the code I had for detecting an existing session was not working; it was not clear why. Nonetheless, I did some research and updated that little code block to “Get-PsSession | Where-Object ComputerName -eq 'myserver.contoso.com'” instead, which I confirmed properly detected that a session was already open. It was just that I then started having this other problem. I don’t think reverting is going to help me, because this function is pretty basic and the only thing that changed was the existing session detection mechanism. If I revert it, I assume I just go back to my previous problem. Looking at the previous code, it was just checking for the $ExchangeSession variable and I am honestly not sure how that ever worked, given scoping in the module.

 

I do appreciate your help as always.

Reply all
Reply to author
Forward
0 new messages