Selecting specific IIS website using powershell - powershell

I currently have several websites running under IIS. For ease lets call them site1, site2, and site3
My powershell script accepts a parameter from the user of which number site to work with. I am attempting to turn off site3 with simply the parameter 3
I thought it would be something along the lines of
stop-website | where-object {$_.name -like "*3*"}
this line is executed, but brought to a prompt for "name"

The Name property seems to accept wildcards (although help states otherwise) so you should be able to stop it with this:
Stop-Website -Name *3*

Related

PowerShell Command to Set Logging on IIS 10

On Windows Server 2016/IIS 10, I can do the following in the IIS Manager GUI with the Log File Format set to W3C:
[Web Server Name] → Logging → Select Fields → W3C Logging Fields (Standard Fields) → Check or uncheck the boxes next to Standard Fields like "User Name (cs-username)"
I would really like to be able to check or uncheck fields like cs-username from a PowerShell script. To that end, I'm trying to discover the path to these standard fields, so I can then set them:
Import-Module IISAdministration
$prop = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST" -Filter /system.applicationHost/sites/sitedefaults" -Recurse
When I run this PowerShell script, it asks me to "Supply values for the following parameters: Name[0].
I think it's a bit funny that it's asking me for a name when I'm trying to discover the next path element or name. If I type in something like cs-username, it comes back with nothing, suggesting I'm in the wrong directory (assuming recurse is actually looking around).
Any thoughts?
This is what ended up working for me for IIS 10:
Set-WebConfigurationProperty `
-filter "/*/*/site/logfile" `
-name "logExtFileFlags" `
-value "Date, Time, ClientIp, etc.."
of course replacing "etc.." with all the desired fields.

Search for and modify a firewall rule in powershell using Get-NetFirewallRule/PortFilter/AddressFilter

What I am trying to do seems simple but I need help knitting all the pieces together.
What I want to do is search all the firewall rules based on local port and protocol (i.e. 3389 TCP) then, if I find one, ensure that the RemoteAddress is set to x.x.x.x. If I don't find one, then add it.
I can't seem to find out how to knit together Get-NetFirewallPortFilter, Get-NetFirewallAddressFilter, and Get-NetFirewallRule to do what I want.
We have a Remote Desktop Gateway and Multi-Factor Authentication and as part of compliance, all RDP connections must go through the RDGateway so that Two Factor is used. There is a rule in place at the firewall but I want to find some way to enforce this on mass using Powershell (in an SCCM compliance item) at the Windows Firewall level too. Sure I could do a Group Policy Objects but I want to be able to report on compliance which is why I am trying to do this via System Center Configuration Manager.
Ugh. I believe this will work. You can pipe these things both ways. I believe it's pretty self explanatory, but it takes 2 minutes on my computer. At least I got a progress bar. The whatif output is actually incorrect. That's the name, not the displayname.
EDIT: Oh I see. It's much faster without the first command. I guess that's the point. I never understood. It's like the -filter parameter to other commands like get-childitem, that make it faster. Get-NetfirewallPortFilter actually returns the name of the firewall rule if you look at all the properties.
# Get-NetFirewallRule |
Get-NetFirewallPortFilter -Protocol TCP |
Where LocalPort -eq 3389 | Get-NetFirewallRule |
Set-NetFirewallRule -RemoteAddress 192.168.1.1 -WhatIf
Output:
What if: Set-NetFirewallRule DisplayName: RemoteDesktop-UserMode-In-TCP
Piping each command to the next takes the input and filters to the end where your result showing the list of Scopes (RemoteAddress) by expanding the selected property, which you can then use to Edit your Set. Each Command shows a subset of the prior one...
Get-NetFirewallRule -DisplayName "Allow Port 3389 - RDP Access" |Get-NetFirewallAddressFilter |Select -expandproperty RemoteAddress

Powershell script check the status of the stopped services and send mail

I am pretty new to Powershell scripting and i have a requirement to check the status of the services, if the services are stopped ,capture the status into a separate text file and send the email status to the users that services got stopped. In my environment services runs on two different machines . How to achieve it using Get -service
Please help me.
Since it looks like you haven't started, I'll give you a few things to kick you off.
Get-Service | Where-Object {$_.status -eq "stopped"}
This is a way to retrieve all the stopped services. Get-Service gets all the services, Where-Object is like an if statement in programming, and the part in parenthesis is the condition to meet.
Here's a link where you can read more about that: https://technet.microsoft.com/en-us/library/ee176858.aspx
This is a line of code that will write processes to a text file:
Get-Process | Out-File c:\scripts\test.txt
You can see how combining different commands will get you the result you want. Search around the internet if you get stuck. PowerShell is pretty well documented.
Here's a link for some more research on writing to a file: https://technet.microsoft.com/en-us/library/ee176924.aspx

Weird Quest PowerShell error

"Move-QADObject : 0000202B: RefErr: DSID-031007EF, data 0, 1 access points"
I get this error when I try the following:
Move-QADObject -identity $results_ep.dn -NewParentContainer "OU=Users,OU=AB,DC=domain,DC=local" -Credential $cred_ep
I am running this script from domain A, and the target domain where the object should be moving is domain B (hence the credentials).
Anyone know whats up?
The default tree in Active Directory is not OU=Users,dc=domain,dc=com but rather cn=Users,dc=domain,dc=com ( CN= not the OU= for Users).
Try change OU=Users with CN=Users
EDIT:
Reading better your question I see just now that you want move object to a different domain,
from get-help move-qadobject:
... omissis ...
Use this cmdlet to move an object between containers within an Active Directory domain (the cmdlet cannot move an object to a different domain).
... omissis ...
Read here to do it with Quest.ActiveRoles.ADManagement sanp-in.
If you are in a Windows Server 2008 R2 DC you can move in a different domain using Windows PowerShell module named Active Directory:
Move-ADObject -Identity "CN=Mary Vick,OU=Accounting,DC=SS64,DC=com" -TargetPath "OU=Accounting,DC=Europe,DC=SS64,DC=com" -TargetServer "server64.europe.SS64.com"
Better of all I think is using native Microsoft commandline like ADMT

How do I pass parameters to a PSUnit test script?

I am using PSUnit for testing purposes in Powershell 2.0. Because my tests need to connect to a database server I would like to be able to pass the server and database name into the test script. This would then allow developers to run the test scripts on their local machine with a local database while at the same time making it possible to run it on a server. The database may also change depending on the environment.
The PSUnit.Run script doesn't seem to allow you to include parameters with the test script name. Have I missed anything? Is there a workaround for this?
Thanks!
The only way that I was able to find to do this was to include tags at the start of my test script, then search and replace the correct values in place of those tags. For example, in my case my test script included this code at the top of the script:
[string]$ServerName=<ServerName>
[string]$TargetDatabaseName=<TargetDatabaseName>
That is the literal code. Then, in my script where I called the tests I included this code:
foreach ($testPSScript in Get-ChildItem "$testScriptDir\*.ps1") {
(Get-Content $testPSScript.FullName) |
ForEach-Object {$_ -replace "<ServerName>", "'$ServerName'"} |
ForEach-Object {$_ -replace "<TargetDatabaseName>", "'$DatabaseName'"} |
Set-Content $testPSScript.FullName -Force
PSUnit.Run.ps1 -PSUnitTestFile "$testPSScript"
}
You have to remember to overwrite your test script(s) with the original version each time, otherwise it will include the values that you used in your last run instead of the tags and you won't be able to change the values.
Why don't you just include the 2 variables ([string]$ServerName & [string]$TargetDatabaseName) in 'profile.ps1' file & use these in your test cases. These would be available there and you can configure them anytime in the ps1 file.
This seems to be easier & more intuitive than writing a separate script for this task.
Just append any such info to PowerShell profile.