Stop IIS-Site and all running sessions / applications - powershell

I want to stop an IIS site, copy my new files / application to it, and then restart the site automated via PowerShell-script.
When I do this via IIS-Manager-GUI (right-click on site --> stop), everything works fine. The site is not reachable, all clients instantly loose their connection (SignalR is used). I replace my files and restart via GUI.
If I try the same with PowerShell Stop-IISSite, the site is not reachable for new requests, but the app seems to run on and existing clients keep working (kind of). If I try to copy my files, app files are still locked and cannot be replaced.
System is a Win2019 Nano Server with IIS 10.0 running an Asp.Net Core 3.1 application (process integrated AppPool).
Here is my PS-code:
Stop-IISSite -Name siteToReplace -confirm:$False;
Write-Host -NoNewLine 'Site siteToReplace is stopped, copy new files now..!';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
Start-IISSite -Name siteToReplace;
What am I missing here? Why does Stop-IISSite work differently than UI? How can I stop a site including running app instances to replace files without having to stop the whole IIS?

So, after some research I used the Stop-Website command, after noticing other commands from the same WebAdministration list were working.
https://learn.microsoft.com/en-us/powershell/module/webadminstration/stop-website?view=winserver2012-ps

First of all you need to install this module Import-Module **IISAdministration**
Then you can have list of sites
PS C:\WINDOWS\system32> Get-IISSite
Name ID State Physical Path Bindings
---- -- ----- ------------- --------
Default Web Site 1 Started %SystemDrive%\inetpub\wwwroot http *:80:
mangos 2 Started E:\DotAng\Release http *:8000:
http *:80:mango.com
banana 3 Started E:\testapi\testApi\WebApplicat http *:8080:
ion1
testapp.com 4 Started E:\testapi\TestApiRelease http *:800:testapp.com
Mango 5 Started E:\testapi\TestApiRelease http *:8001:
Stop specific website
Stop-IISSite -Name "Mango"
Starting specific website
Start-IISSite -Name "Mango"

Related

Set 'environmentVariables' for different sites in IIS automatically in azure pipeline

I have release with pipeline that have couple 'IIS web app deploy' jobs that deploys one ASP.NET Core web API application in two sites in IIS in my agent (it needs for 2 different regions).
After this I need to setup different environmentVariables for each site. I used to do this manually (something like this). Now I'm trying to do it automatically. I wrote PS script that works fine in my agent machine:
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/MY_SITE_NAME' -filter "system.webServer/aspNetCore/environmentVariables" -name "." -value #{name='ASPNETCORE_ENVIRONMENT';value='my_value'}
I'm trying to run this script as 'PowerShell v2 task' after IIS deployment but it has no effect:
Any ideas how to set different 'environmentVariables' for different sites in IIS automatically?
It turned out to work correctly. I was experimenting on an old release, and all just I had to do was create a new one.

Changing default directory and default html page for IIS through Powershell

I am looking to use Powershell to programatically change the default directory (and the home page - a simple static html) of IIS. For example, I created a new Drive 'D:\' and placed an 'index.html' and I would like IIS to launch this html instead of "C:\inetpub\www\iisstart.htm".
I can do this switch manually by RDP'ing into the machine but I need to achieve this using Powershell.
I am not sure if this is the way to do it but I tried this:
Set-ItemProperty 'IIS:\Sites\Default Web Site\' -name physicalPath -value "D:\"
The command doesn't throw any error but I get 500 - Internal server error.
If I check using another command as below, I get this displayed. But when I type the public IP of this machine the earlier page displaying content of C:\inetpub\www\iisstart.htm vanishes and I get 500 server error.
PS C:\Users\Administrator> Get-Website -Name 'Default Web Site'
Name ID State Physical Path Bindings
---- -- ----- ------------- --------
Default Web Site 1 Started D:\ http *:80:
I am pretty sure there must be a very simple way to do this but I couldn't find out what it is, in spite of hunting for hours together. Your help would be much appreciated!
Please note I am only trying to host this simple static html page for now (and nothing more).

Deploying WSP remotely from PowerShell 3.0

I have this large application that I am using Windows RM 3.0 to deploy Databases, SSIS packages, and other things to multiple different servers and it is working just fine. It was requested that branding changes to a Business Intelligence SharePoint site be added to this process as well. So I create a custom build script to do so, and set Win RM to run this command from PowerShell on Sharepoint server
Install-SPSolution –Identity Payload\SharepointDeploy.wsp –WebApplication http://localhost/ -GACDeployment
when I run that, I get the following error
Install-SPSolution : Microsoft SharePoint is not supported with version 4.0.30319.18444 of the Microsoft .Net Runtime.
Reading around, it seems its a PowerShell 3.0 issue and when running in 2.0 it works fine. However, my existing process requires PowerShell 3.0 to work properly. Is there anyway to get this working with 3.0? Or can I spin up a 2.0 instance using an Invoke-Command or something? I can provide more details if needed.
You can build a custom endpoint which will run the required version of PowerShell. That way you won't have to mess with the default endpoint which you probably want to keep with its defaults
This would require you to connect to the new endpoint with something like
new-pssession -computername "SharePoint01" -configurationName "psv2".
You build and endpoint with the following cmdlet:
New-PSSessionConfigurationFile -Path "psv2session.pssc"
And then register an endpoint using that config with this cmdlet:
Register-PSSessionConfiguration -Name psv2 -Path psv2session.pssc –ShowSecurityDescriptorUI
It's fairly easy to do, and this link provides a good introduction to the setup:
http://blogs.technet.com/b/heyscriptingguy/archive/2014/04/02/build-constrained-powershell-endpoint-using-configuration-file.aspx (although the blog deals with constrained endpoints, the teqnique is essentially the same for what you need to do)
I was facing the same issue with console application, I decreased the framework version from 4.5 to 3.5 from the project properties page and I works perfect!

Script to get Windows 7 backup status of multiple client computers and send email if one fails

I have looked around and not found anything about remotely checking Windows 7 backup status.
We have Windows 2008 R2 SBS running our domain with 5 Windows 7 client computers. Each client computer is backing up to a NAS (some programs we have are a huge pain to re-install if a hard drive dies, so we have a system image of each). I would like to run a PowerShell script that checks each client computer for a successful backup and if one has failed, send an email.
What I need help with the most is the part to query each computer for backup status.
There are so many way you can approach this problem. Here is one way:
You can schedule a job on each computer that runs a script which checks the status code of the backup job and if it detects failure send an email.
Now? How do you get the task results? You might use something like this (not tested)
$s = New-Object -com Schedule.Service
$s.connect
$au = $s.getfolder('').gettasks(0) | where {$_.name -match 'automaticbackup'}
if ( $au.LastTaskResult -ne 0) {
##send email
}
Depending on the version of the PowerShell you can, for example, use 'send-email' cmdlet.
Hope this helps get you started.

PowerShell Stop-Service/Start-Service not working on a specific server

I have three servers, let's call them Deploy1, Deploy2, Target.
All servers are running Windows Server 2008R2, fully updated.
A domain user, admin1, is configured as administrator on all servers, and this is the user I'm running all the commands with.
The following command works on Deploy1:
Get-Service "MyService" -ComputerName Target | Stop-Service
When running the same command on Deploy2, the command fails with the following message:
Cannot find any service with service name 'MyService'.
On Deploy2, the following command works, and displays the service and its status.
Get-Service "MyService" -ComputerName Target
Now, I know there are other ways to stop/start services via PowerShell, but I like this one as it automatically waits for the server to actually stop/start.
So what could be wrong with Deploy2?
Powershell v2.0 has a bug (feature?) in how the object returned by Get-Service is implemented. It does not actually set the ComputerName property correctly. Because of this, it can only affect local services. If you upgrade to Windows Management Framework 3.0 (and consequently Powershell v3) the bug is fixed and will work correctly.
Does this work? If not, is there an error produced?
(Get-Service "MyService" -ComputerName Target).Stop()