Monthly Archives: December 2013

Redirecting output streams in Powershell

Found this on MS connect site discussion and reposting here because I don’t know about you but I can never remember the syntax

Posted by Microsoft on 15.2.2012 at 16:40

 

# In PS 3.0, we've extended output redirection to include the following streams:
# Pipeline (1)
# Error    (2)
# Warning (3)
# Verbose (4)
# Debug    (5)
# All     (*)

# We still use the same operators
# >    Redirect to a file and replace contents
# >>    Redirect to a file and append to existing content
# >&1    Merge with pipeline output

# First, let's set some preference variables and get a temp file
$VerbosePreference = "Continue"; $DebugPreference = "Continue"; $filename = [System.IO.Path]::GetTempFileName()

# Scenario 1
# Merge warning output with pipeline output (3>&1)
# Pipeline output is assigned to a variable
# The error output goes to the host (no redirection)
$var = $(Write-Output "Pipeline Output"; Write-Warning "Warning Output"; Write-Error "Error Output") 3>&1
# The variable $var contains the pipeline and warning output
$var

# Scenario 2
# Redirect warning output to a file (3>$filename)
# All other output is merged with pipeline output (2>&1 4>&1 5>&1)
$var = $(Write-Output "Pipeline Output"; Write-Warning "Warning Output"; Write-Verbose "Verbose Output"; Write-Error "Error Output") 2>&1 4>&1 5>&1 3>$filename
# Pipeline, Verbose, and Error output are now in $var
$var
# Warning output is in the file
Get-Content $filename

# Scenario 3
# Redirect all output to a file (*>$filename)
$(Write-Output "Pipeline Output"; Write-Warning "Warning Output"; Write-Verbose "Verbose Output"; Write-Error "Error Output"; Write-Debug "Debug Output") *>$filename
# All output is now in the file
Get-Content $filename

# Scenario 4
# Merge warning, error, and verbose output with the pipeline output (2>&1 3>&1 4>&1)
# Redirect the pipeline output to a file (1>$filename)
# Debug output goes to the host (no redirection)
$(Write-Output "Pipeline Output"; Write-Warning "Warning Output"; Write-Verbose "Verbose Output"; Write-Error "Error Output"; Write-Debug "Debug Output") 2>&1 3>&1 4>&1 1>$filename
# All other output is now in the file
Get-Content $filename 

One recent example from me:

# redirect all but debug stream into a file
# Sharepoint 2013 install - keeping for future reference
Initialize-SPResourceSecurity -Verbose 2>&1 3>&1 4>&1 1> C:\Install\install_logs\Initialize-SPResourceSecurity.txt

Frequently Asked Questions About Passwords

Frequently Asked Questions About Passwords.

Smart Card Group Policy and Registry Settings

Smart Card Group Policy and Registry Settings.

How To Create FAQ Site in SharePoint Using Standard SharePoint Lists | Mantraaz

How To Create FAQ Site in SharePoint Using Standard SharePoint Lists | Mantraaz.

Windows Server 2008R2 shows LUNs from SAN as offline

After adding disk to a WmWare VM in VSphere client I wanted to initialize and format it (as always) in Win 200R2 disk Management MMC snap-in… only to find out that disk id unknown and offline. A little info message says that “the disk is offline because of policy set by an administrator”. When right clicked the disk presented no context menu.

disk manager

no_context_menu

Resolution:

1.

run cmd as Administrator

2. run diskpart – use these commands

C:\Windows\system32>diskpart

Microsoft DiskPart version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: S7022DB0101

DISKPART> san

SAN Policy  : Offline Shared

DISKPART> san policy=onlineall

DiskPart successfully changed the SAN policy for the current operating system.

DISKPART> san

SAN Policy  : Online All

DISKPART> list disk

  Disk ###  Status         Size     Free     Dyn  Gpt
  ——–  ————-  ——-  ——-  —  —
  Disk 0    Online          100 GB  1024 KB
  Disk 1    Offline         250 GB   250 GB
  Disk 2    Offline         100 GB   100 GB

DISKPART> select disk 1

Disk 1 is now the selected disk.

DISKPART> attributes disk
Current Read-only State : Yes
Read-only  : Yes
Boot Disk  : No
Pagefile Disk  : No
Hibernation File Disk  : No
Crashdump Disk  : No
Clustered Disk  : No

DISKPART> attributes disk clear readonly

Disk attributes cleared successfully.

DISKPART> attributes disk
Current Read-only State : No
Read-only  : No
Boot Disk  : No
Pagefile Disk  : No
Hibernation File Disk  : No
Crashdump Disk  : No
Clustered Disk  : No

3.

Go back to Disk management snapin > menu Action > Rescan Disks

4.

Right-click disk in offline state and select Online from the context menu

set_disk_online_in_disk_manager

Creating Filters in Recipient Commands: Exchange 2010 Help

Creating Filters in Recipient Commands: Exchange 2010 Help.

Filterable Properties for the -RecipientFilter Parameter in Exchange 2007 SP1 and SP2: Exchange 2007 Help

RequireAllSendersAreAuthenticated

via Filterable Properties for the -RecipientFilter Parameter in Exchange 2007 SP1 and SP2: Exchange 2007 Help.

smsagent

Tips, tricks and time-savers for the Windows and ConfigMgr administrator

To The Point

Anything about Technology and Business

Brian's Power Windows Blog

Microsoft in the Enterprise. Windows, Hyper-V, Exchange, SQL, and more!

PowerScripting Podcast

Shownotes and links for the PowerScripting Podcast, a podcast to help people learn Windows Powershell

Learn Powershell | Achieve More

What is this Powershell of which you speak?