Identify unused application pools - powershell

I intend to find out the unused application pools among tens of live app pools on each server in a web farm containing a number of servers. The app pools are isolated per website and application.
I have listed out the application pools per server using a PS script - I'm a newbie btw, however I'm unable to match the IIS logs against the website ID for the corresponding application pool to determine if any traffic is served by the website/application at all.
This is mainly for housekeeping reasons. Is there any better approach I can take to automate the entire process?

One would think this would be easier--perhaps there is an easier way, but the following is what I was able to come up with. I have never used PowerShell to manage IIS so it's likely that someone who has more expertise in the area will have a more concise approach.
import-module WebAdministration
# get a list of the application pools
$pools = dir IIS:\AppPools
# iterate through the pools and find the ones that are not used by any websites
foreach($pool in $pools){
if ((Get-WebConfigurationProperty "/system.applicationHost/sites/site/application[#applicationPool='$($pool.name)']" "machine/webroot/apphost" -name path).Count -eq 0)
{
write-host $pool.name "has no websites."
}
}

Related

Managing Disk Quotas in Powershell Remotely

I'm trying to eventually create a winform powershell tool that manages disk quotas on a Windows 2016 server from a Windows 10 client workstation. Given that Every single user has at least 2 mapped drives is there any way to query a specific user and volume without get-wmiobject cycling through every single quota first? The goal is to create a tool as fast or faster than just remoting into the file server to increase quotas as requests come in but to increase a quota we need to know the quota limit first.
I also know that I could map the root of a drive to a letter using my admin creds, but we don't allow saving credentials for mapped drives because we have to follow guidelines set up by federal government (State Health Care Agency) and our security team doesn't allow for credentials to be saved in any kind of windows logon. So that being said the powers that be are wanting me to find a way around us remoting into the server with our admin credentials and increasing quotas.
We don't use FSRM on the server but instead manage quotas for each volume. That decision was made before my time.
For testing I have tried using the following:
$query = "Select * from win32_diskquota where QuotaVolume=""Win32_LogicalDisk.DeviceID='$volume'"" AND User =""Win32_Account.Domain='$domain',Name='$Username'"""
Get-WmiObject -Query $query -ComputerName server
This one I knew was going to take forever, but tried it anyway:
Get-WMIObject Win32_DiskQuota -ComputerName $computer | where {$_.deviceid -eq "Win32_LogicalDisk.DeviceID=$drive" -and $_.user -eq "Win32_Account.Domain=$Domain"+",Name=$name"}
I've also used get-ciminstance just to see if it would compile the list of quotas from the remote server any faster, but no dice.
Any suggestions, hints, or advice would be great. Thanks in advance.

Running BitsTransfer from Local Service account

I am working on making some scripts to make my job a little bit easier.
One of the things i need is too download some files to use. I first used powershell with the command Invoke-WebRequest.
It is working really well, however it dont run on windows 7 computeres, as they have powershell 2. As i have about as many windows 7 pc's as win 10 i need to find another way.
I found that Start-BitsTransfer is a good way that should work on most computeres. My problem now is, that when using the script via my remote support session it runs the script on the local service account, and then BitsTransfer wont run and gives me an error. (0x800704DD)
Is there a way to get around that problem, or any command that can be used on both win 7 and 10 and run from the local service account?
You should update PowerShell as gms0ulman states, but if you are not the person who is in charge of this decision, you have to take other steps.
This error code...
0x800704DD
The error message ERROR_NOT_LOGGED_ON, occurs because the System Event Notification Service (SENS) is not receiving user logon notifications. BITS (version 2.0 and up) depends on logon notifications from Service Control Manager, which in turn depends on the SENS service. Ensure that the SENS service is started and running correctly.
By default, BITS runs under the LocalSystem account. To modify, stop or restart BITS, you must be logged on as an administrator. In your situation, when you log on a regular account and start the PS in elevated privilege, the BITS doesn’t run under regular user account. To resolve it, you may need to configure the log on user for BITS. Please visit the following link to configure how a service is started.
Configure How a Service is Started
Services are often run with default settings — for example, a service
may be disabled automatically at startup. However, you can use the
Services snap-in to change the default settings for a service. This is
useful if you are troubleshooting service failures or if you need to
change the security account under which a service runs. Membership in
Account Operators or Domain Admins, Enterprise Admins, or equivalent,
is the minimum required to complete this procedure. Review the details
in "Additional considerations" in this topic.
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc755249(v=ws.10)
I also agree that you should not continue supporting PowerShell 2.0. Ideally, ditch Windows 7 (it's way too old now), if you can't do that, upgrade PowerShell, if you can't do that, find a new job, if you can't do that, then I guess bring on the workarounds!
postanote's answer covers the BITS angle.
The other thing you can do is just use the .Net framework's underlying libraries, which is exactly what Invoke-RestMethod and Invoke-WebRequest do (those cmdlets were introduced in PowerShell 3.0, but the guts of them were around much longer).
try {
$wc = New-Object -TypeName System.Net.WebClient
$wc.DownloadFile($url, $path)
finally {
$wc.Dispose()
}
Most people don't bother disposing IDisposable objects in PowerShell so you'll see a lot of shorthand around like this:
(New-Object Net.WebClient).DownloadFile($url, $path)
Which is probably fine if your script's process isn't going to be around for a while, but it's good to keep in mind in case you incorporate this into something of a larger scale.

Get the machine name of an Azure worker or web role using PowerShell?

Is there any way using the PowerShell Azure cmdlets to get the machine name on which an Azure worker or web role is running? Specifically, I'm looking for the name that starts with "RD". I'm not 100% sure if I'm searching for this using the right terminology, because my results are clouded with information about Azure Virtual Machines. I've also been exploring the objects returned from such calls as Get-AzureDeployment and Get-AzureVM, but haven't found the "RD" name anyplace yet.
I've also found the discussion here, but wondering if it's out of date: http://social.msdn.microsoft.com/Forums/windowsazure/en-US/73eb430a-abc7-4c15-98e7-a65308d15ed9/how-to-get-the-computer-name-of-a-webworker-role-instance?forum=windowsazuremanagement
Motivation: My New Relic monitoring often complains "server not reporting" for instances that have been decommissioned. New Relic's server monitoring knows only the "RD..." names, and I'm looking for a quick way to get a list of these from Azure so that I can compare and see if New Relic is only complaining about old instances or if there's a real problem with one of the current instances.
You can actually get more significant host names than RD... by setting the vmName key in the cloud service's ServiceConfiguration file.
Then, your host names will be of the form vmnameXX, where XX is the instance number of the role. (i.e. "MyApp01", "MyApp02", ...)
For details on this, see the links below:
https://azure.microsoft.com/documentation/articles/virtual-networks-viewing-and-modifying-hostnames/
http://blogs.msdn.com/b/cie/archive/2014/03/30/custom-hostname-for-windows-azure-paas-virtual-machines.aspx

Retrive application properties from SCCM with script during a deployment

In our environment we deploy some applications with a Powershell wrapper in rare cases. For logging purposes, we want the script to retrieve properties from SCCM during installation. The properties in question are name, version and vendor. Doing some research, I figured out that I get a instance of the CCM_Application from the SCCM 2012 Client SDK:
Get-WmiObject -Namespace "Root\CCM\Clientsdk" -Class "CCM_Application" | Where {$_.EvaluationState -eq 12}
By looking for the EvaluationState value 12, I find the application in Software Center that is being currently installed. This works great for applications deployed to devices. However; when running it with applications deployed to users, it doesn't return anything. Doing some research, I discovered that CCM_Application is user centric, and the privileged service account running the script doesn't have a instance of the application.
Is there a way of making the above code work with applications deployed to users? Also; is there a better way of somehow retrieving properties from ccmexec during execution?
I know this is a very old post. The only way around this I know would be to create a scheduled task via group policy that runs as the logged in user. Trigger that, have it write it to a file, and then read it.
We were doing similar work, and we were able to get what we needed by reading appenforce.log. As crude as that is.

Stop/Start Service hosted in IIS using Powershell or WMI

I Have some WCF services hosted in iis. Using powershell script i want to stop and start individual services(or applications).i found so many articles for stop App-pool.but i want a specific application to be stopped.And i have heard that with WMI we can make it.But i could not find any relevant script for that.
In IIS, you can only stop an "Application" by stopping it's associated Application Pool. Thus the necessity to stop a specific application would require you to organize your applications into Application Pools such that you would only stop a single (or group) of applications when stopping a specific Application Pool.
By organizing your Applications into multiple Application Pools, you can thus utilize the app-pool.vbs or Powershell or WMI interfaces to stop the specific AppPool associated with the application you wish to stop.