Archive

Posts Tagged ‘PowerShell’

PowerShell: About SessionConfiguration and how to change them

April 24th, 2010 christian No comments

Remote Access Without Admin Privileges

In PowerShell v.2, remote access is available only to users who hold local administrator privileges. So, even if you do have appropriate remote access to a machine, , you cannot remotely access the system if you are not an Admin. This is not a technical limitation, though, just a safe default. You should use this line to change it :

Set-PSSessionConfiguration -Name Microsoft.PowerShell -showSecurityDescriptorUI

Categories: PowerShell Tags: PowerShell

PowerShell: Display almost anything in a graphical GridView

April 24th, 2010 christian No comments

Out-GridView Dirty Tricks

Out-Gridview is a new cmdlet in PowerShell v.2 which allows you to output objects to a “mini” excel sheet like this:

Get-Process | Out-GridView

However, this only works if .NET Framework 3.51 is installed. While PowerShell requires just .NET 2.0, both Out-GridView and ISE-Editor require 3.51 because of their WPF graphical output.

Out-Gridview will display the same columns you would have seen when you output the objects to the console. You should clone the object first to see all object properties in your gridview:

Get-Process | Select-Object * | Out-Gridview

Categories: No Category Tags: PowerShell

PowerShell: Create a temporary filename

April 24th, 2010 christian No comments

Thanks to Get-Date, you can easily create unique temporary file names with a timestamp:

(Get-Date -format ‘yyyy-MM-dd hh-mm-ss’) + ‘.tmp’

Categories: No Category Tags: PowerShell

PowerShell: Generate PC Lists using Foreach-Object and -f Operator

April 24th, 2010 christian No comments

Generate PC Lists

One easy way of creating lists of PC names or IP address ranges etc is a simple pipeline like this:

1..40 | Foreach-Object { ‘PC-W7-A{0:000}’ -f $_ }

Use the -f operator to format the number. In this example, it will always be three-digit.

Categories: PowerShell Tags: PowerShell

PowerShell: Sort-Object

April 24th, 2010 christian No comments

You can use Sort-Object to sort simple variable types. Have a look at the following:

‘Tom’, ‘Chris’, ‘Judy’, ‘Alan’ | Sort-Object

Input can come from a different command. If you’d like to get seven random lottery numbers, you should try this:

1..49 | Get-Random -Count 7 | Sort-Object

You’ll find that when you feed complex objects into Sort-Object, you should specify the object property you want to sort on (or else Sort-Object will pick one by itself):

Get-ChildItem $env:windir | Sort-Object Length

Here are some more examples:

Get-HotFix | Sort-Object InstalledOn

Get-ComputerRestorePoint | Sort-Object Description

Categories: PowerShell Tags: PowerShell

PowerShell: Creating large dummy files with .NET

March 13th, 2010 christian No comments
Ok, this one’s is cool i think. Whenever i needed a large dummy file, i opened up notepad, write ASDFASDFASDFA CTRL-A, CTRL-C, CTRL-V,CTRL-V,CTRL-V,CTRL-V. CTRL-A, CTRL-C, CTRL-V,CTRL-V,CTRL-V,CTRL-V,CTRL-V.. Then i saved the file, opened up a CMD-Shell and wrote copy dummyFile.txt+dummyFile.txt+dummyFile.txt until the file was large enough.. ok, there’s fsutil.exe which does the same in a much more efficient manner.. OR, unleash the Power of .NET – no, you don’t have to start programming using Visual Studio, just open up PowerShell.. Logo_PowerShell

Read more…

Categories: PowerShell Tags: PowerShell

PowerShell: Running PowerShell Scripts as Scheduled Task

March 10th, 2010 christian No comments
Schtasks.exe – Enables an administrator to create, delete, query, change, run, and end scheduled tasks on a local or remote computer. Running Schtasks.exe without arguments displays the status and next run time for each registered task.

If you have jobs that need to execute regularly, you can manage them with a PowerShell script and make it a scheduled task:

Logo_PowerShell

Read more…

Categories: PowerShell Tags: PowerShell, Scheduled Task

PowerShell: Delete specific files recursively / recursive

February 19th, 2010 christian No comments
Okay, this post I’m going to write down for my personal notes but also for you..
Ever needed to delete a specific file, in my case in need to delete (remove-item) all *.pdb files in one of my Visual Studio Solution.. recursive of course.
To get a list of Files use the Get-ChildItem CmdLet. Extend that command using the –rec or –recurse parameter – so you’ll get all the files recursively, starting from the current path / directory.
Logo_PowerShell

Read more…

PowerShell: Enumerate Files recursive and get full path for each file

July 29th, 2009 christian No comments

To enumerate all files of a specific path recursive and get the full path for each file using Microsoft PowerShell:


Get-ChildItem -rec | ForEach-Object -Process {$_.FullName}

to capture the output and generate an XML file do:


Get-ChildItem -rec | ForEach-Object -Process {$_.FullName} | Export-Clixml c:\temp\report.xml

to only enumerate / list files, use the psIsContainer switch:


Get-ChildItem -rec | Where-object {!$_.psIsContainer -eq $true} | ForEach-Object -Process {$_.FullName}

or for folders only:


Get-ChildItem -rec | Where-object {!$_.psIsContainer -eq $false} | ForEach-Object -Process {$_.FullName}

Categories: Microsoft, PowerShell Tags: list, PowerShell

PowerShell: UAC, SUDO, Elevation, Access to the path ‘C:\Program Files’ is denied.

July 26th, 2009 christian No comments

Powershell on Windows Vista and Windows 7 runs restricted and not-elevated by default. Microsoft did a good job when they invented the so called UAC (User Account Control). As ususal, Microsoft started something new and did not finish it yet – the product is still not feature-complete. Well, probably it’s their philosophy to create thousands of products in a vertical manner, the user then is left all alone finding out how to dive into the features from a horizontal perspective…

What i mean: PowerShell runs restricted and has NO SUDO command. For sake of christ, there is a workaround.

First, find out the location of your profile.ps1. it easy as:


PS C:\> $profile
C:\Users\ PS C:\>

Probably this script does not exist by default. At least on my systems this was the case. So create it.
When created / if it does exist do:


PS C:\> notepad.exe $profile

This should open your profile.ps1 script in notepad..
Edit this Script to include:


function elevate-process
{
$file, [string]$arguments = $args;
$psi = new-object System.Diagnostics.ProcessStartInfo $file;
$psi.Arguments = $arguments;
$psi.Verb = "runas";
$psi.WorkingDirectory = get-location;
[System.Diagnostics.Process]::Start($psi);
}

set-alias sudo elevate-process;

Now you’re almost done. After starting a new PowerShell session – even a non-elevated one – you can run commands elevated by using the sudo command.
For Illustration:

PS C:\> sudo notepad.exe

If the new PowerShell session should display a warning regarding the execution of scripts, check out my older posts (using the site search), i’ve postet a solution for that.

WordPress SEO fine-tune by Meta SEO Pack from Poradnik Webmastera