Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Wednesday, June 13, 2018

PowerShell Tips

I'll likely update this as I go along


Create a file like you would with touch

echo $null > filename

Thursday, March 23, 2017

tail on Windows with PowerShell

One of the well known Unix/Linux commands is tail, which gets the tail of a file, meaning the end of a file and prints it out to the output stream.

I used to try find nice programs for this, including Baretail which has some very nice features like colouring lines in that match certain patterns etc.

But if you just want a simple, and now built into Windows solution, just use PowerShell:

Get-Content -Tail 10 filename.txt

This will show the last 10 lines of the file. You can even follow the tail, or watch it for changes, as below:

gc -Tail 10 -Wait filename.txt

Another nice thing you can do is then pipe this into Select-String to filter the output:

gc -Tail 10 -Wait filename.txt | Select-String -Pattern somepattern

Wednesday, January 25, 2017

Uninstall Windows Service using PowerShell

You can get a list of services using the Get-Service cmdlet, for example, to retrieve a list of all running services you could run:

Get-Service | Where-Object { $_.Status -eq "Running" }

You could even retrieve a specific service and start/stop it:

$service = Get-Service DemoService
$service.Start()
$service.Stop()

However, there is nothing to remove the service on this object.

We can also use GetWmiObject to do the same things:

$service = Get-WmiObject Win32_Service -Filter "name='DemoService'"
$service.StartService()
$service.StopService()

But this WMI object actually also exposes a delete functionality, which will mark the service to be deleted.

$service = Get-WmiObject Win32_Service -Filter "name='DemoService'"
$service.Delete()

If there are resources that are being held onto, it will only be deleted once they're freed up - whether it's the service that needs to gracefully shutdown still, or maybe the services manager snap-in (services.msc) has an open properties window open (I think that's happened to me before), and closing it then puts the delete through.

Friday, March 18, 2016

Set up IIS using PowerShell

When you're setting up a new server there are sometimes steps you need to do before you can configure your website, such as turning on Windows Authentication in corporate environments, or perhaps installing the features necessary for an ASP.NET application. I had to do this recently when setting up new servers at a client:
404 - Image Not Found
404 - Image Not Found
404 - Image Not Found
There was this final screen though where I noticed that this configuration can be exported to an XML file and set up via PowerShell. When inspecting the XML that was generated however, it seemed to include specific host names and so may not be a very generic way of setting up a server. After a bit of investigation, there are other commands that one can run to view what is installed, as well as install specific features.
404 - Image Not Found
404 - Image Not Found
Notice too that it seems to install other dependent features by default, as I just specified ASP.NET 4.5 but the other related features were also installed (in the GUI version you would be prompted about this).
Could be quite useful to have this as a script instead of remembering which features to turn on and off. My PowerShellFu isn’t the strongest, but something along the lines of this should work (I formatted this on separate lines for readability, you will want to run it as a single line):

Get-WindowsFeature -Name Web-*
  | Where-Object
  {
    ($_.Name -eq "Web-Asp-Net45" -or $_.Name -eq "Web-Windows-Auth")
    -and
    $_.InstallState -ne "Installed"
  }
  | Install-WindowsFeature

This may be a bit more generic, and could be used as a script included in source control for setting up new servers, instead of manually doing this via the Server Manager. There are also more advanced options to the cmdlets, more information of which can be seen in the documentation, one example of which is specifying a ComputerName.
One scenario I could think of for this is when setting up a web farm, and perhaps you have multiple hosts you need to do this on. Instead of doing each separately, you could probably pipe in the hostnames to a setup script, and let it perform the setup on each host. Though, as said, my PowerShellFu isn't strong, and I've had issues setting up remote PowerShell admin before, so I'll leave that part up to you ;)

Tuesday, May 20, 2014

Getting the Event Log from another machine with PowerShell

If you have to manage a server farm of more than one computer, PowerShell can be of use if you have access to it.

If you don't have access to it, you may have to setup PowerShell Remoting (don't ask me how, because I haven't done that myself yet).

PS C:\> Get-EventLog -ComputerName somecomputer -LogName Application -Newest 10

Note that I'm using the -Newest paramter and not using the -After parameter, because apparently this means you will need to wait for PowerShell to go through the entire log, instead of stopping at a certain point.

Using a specific version of PowerShell

If you inspect $PSVersionTable you'll see a few different versions of things being used with PowerShell in the current session, specifically look for $PSVersionTable.PSVersion. By default the most latest version will be used.

To launch a specific version of PowerShell you can pass the -version switch, e.g. for PowerShell v2:

c:\ powershell -version 2






Wednesday, October 9, 2013

Die Skype!

Every now and then Windows starts up with Skype running but not in the system tray.

For the life of me, I don't know why. The only way to then go into Skype is to 'launch' it again, which brings the app into focus and on the Taskbar. But then, since I minimize to the system tray and don't keep it in the Taskbar to save space, it gets lost again :(

It's also a pain opening up the Task Manager just to close Skype. So, the solution, kill skype!

    Start -> Run -> powershell -command "ps skype | kill"