Winnt.Sif:
[GuiRunOnce]
%systemdrive%\install\install_applications.cmd
install_applications.cmd:
... other application installation statements...
...
ECHO.
ECHO Installing PowerShell
ECHO Please wait...
start /wait %systemdrive%\install\applications\WindowsXP-KB926139-x86-ENU.exe /quiet /passive /norestart
exit
install_applications.cmd:Now PowerShell should install without a problem...
ECHO.
ECHO Installing .NET Framework 2.0
ECHO Please wait...
start /wait %systemdrive%\install\applications\DotNet\2\dotnetfx.exe /q:a /c:"install.exe /qb"
ECHO.
ECHO Installing PowerShell
ECHO Please wait...
start /wait %systemdrive%\install\applications\WindowsXP-KB926139-x86-ENU.exe /quiet /passive /norestart
When PowerShell was still called Monad, there was a PDC05 Hands-On Lab document available with Monad distribution. The document was about "Creating Monad Providers" for Access database.
I have started to go over the old PDC 05 document since I could not find enough documentation to get started. The document was a bit out-dated but it was not hard to figure out what's been changed since Monad beta 2.
Patterns and utility methods are about the same as what's used for creating AccessDb provider in the lab. But the problem was that, I wanted to be able to create a provider-specific parameter for my PsProvider. (Let me get back to this a bit later on)
For e.g.). Under Certificate provider, Get-ChildItem has a Certificate-Provider specific parameter -CodeSigningCert for Get-ChildItem.
Now let's see how one can create a "sys" drive for System EventLog
You can retrieve System event log entries using Get-ChildItem cmdlet(or ls, or dir whichever alias you prefer):
ls sys:
Above is equivalent to:
Get-EventLog -logName System
Both ls sys: and Get-EventLog System returns all event log entries in System EventLog(well as you might or might not have guessed ;)) and not only they display the same content, but the output should look exactly the same.
The reason that output look exactly the same is because there is a pre-defined display format for the type System.Diagnostics.EventLogEntry in $PsHome/dotnettypes.format.ps1xml.
Now, back to the provider-specific parameter for Get-ChildItem, my implementation takes in Index of event log entry. But I would also like to supply additional parameters so that Get-Item can retrieve event log entry based on either EventID or InstanceID like the following
Get-Item -EventID 18
Get-Item -InstanceID 198397
Anyways, here is the current implementation of Get-Item in action
(201..205) is an array holding indexes from 201 through 205 so EventLogEntry objects with Indexes between 201 and 205 are displayed. How neat. I prefer that over
get-eventlog system | ? { ($_.Index -ge 201) -and ($_.Index -le 205) }
since there is no need to specify which log to list event log entries from nor use clunky syntaxes for Index ranges. Well, one can live with above syntax but when it comes to scattered index ranges you want to select, the latter approach gets a bit too uhm, unpractically complicated. Try accomplish ls (1..5+10..15) using Get-EventLog, it doesn't look pretty...
Lastly, Xaegr on IRC has suggested that it wouldn't hurt to add a support to get remote machine's EventLog entries. Xaegr has suggested a UNC path like syntax "\\MachineName\LogName" for the Root string, but I just went with MachineName:LogName for a quick and dirty testing... Now, this functionality is quite buggy... so when I tried to use IP instead of hostname, I get errors like Get-Item cannot find network path, blah... Moreover, you should have already impersonated or authenticated to the remote machine before creating new EventLog PsDrive for the remote machine and must have appropriate privileges to read EventLog. Since I am not systems administrator, this is a bit tough to understand...
Well, I would appreciate it if someone could answer my question on NG(PowerShell News Group)
W00t, My first post using Windows Live Writer... Took me really long to get used to this a bit.
Tags : Monad MSH PowerShell
[^_^]PS[217]>$str = "abc" -as [psobject]
[^_^]PS[218]>$str | Add-Member -MemberType ScriptProperty -Name ToBase64String `
>> -Value {[System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($this))}
>>
[^_^]PS[219]>$str.ToBase64String
YQBiAGMA
[^_^]PS[220]>$base64 = $str.ToBase64String
[^_^]PS[221]>$base64
YQBiAGMA
[^_^]PS[222]>$base64 | Add-Member -MemberType ScriptProperty -Name FromBase64String `
>> -Value {[System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($this))}
>>
[^_^]PS[223]>$base64.FromBase64String
abc
$str.ToBase64String
$str.FromBase64String
[^_^]PS[258]>$str = "abc" -as [PsObject]
[^_^]PS[259]>$str | Add-Member ScriptProperty ToBase64String `
>> {[System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($this))}
>>
[^_^]PS[260]>$str | Add-Member ScriptProperty FromBase64String `
>> {[System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($this))}
>>
[^_^]PS[261]>$str.ToBase64String
YQBiAGMA
[^_^]PS[262]>$str.ToBase64String.FromBase64String
[^_^]PS[597]>Backup-Database -dbName:pubs -deviceNames:'d:\bak\pubs.bak' -deviceType:DontKnow
Backup-Database : Cannot convert value "DontKnow" to type "Microsoft.SqlServer.Management.Smo.DeviceType" due to invali
d enumeration values. The possible enumeration values are "LogicalDevice, Tape, File, Pipe, VirtualDevice".
At line:1 char:73
+ Backup-Database -dbName:pubs -deviceNames:'d:\bak\pubs.bak' -deviceType:D <<<<>
# author: Sung Kim
# Description: Create Full or Differential backups for
# SQL Server database with SQL Server Objects(SMO) using PowerShell
function Backup-Database {
param([string]$dbName = $(throw 'Enter Database name to backup'),
[string[]]$deviceNames = $(throw 'Enter device name(s)'),
[Microsoft.SqlServer.Management.Smo.DeviceType]$deviceType =
[Microsoft.SqlServer.Management.Smo.DeviceType]File,
[Microsoft.SqlServer.Management.Smo.Server]$server,
[switch]$Overwrite = $false,
[switch]$Differential = $false)
trap { break; }
# Load SMO assembly
$PRIVATE:SmoDir =
'C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies'
[System.Reflection.Assembly]::LoadFile(
"$PRIVATE:SmoDir\Microsoft.SqlServer.Smo.dll") | out-null
# Initialize backup object
$PRIVATE:bkp = New-Object Microsoft.SqlServer.Management.Smo.Backup
$PRIVATE:bkp.Database = $dbName
$PRIVATE:bkp.Initialize = $Overwrite
$PRIVATE:bkp.Incremental = $Differential
# Add backup devices to backup database to
foreach ($PRIVATE:deviceName in $deviceNames) {
$PRIVATE:bkpItem =
new-object Microsoft.SqlServer.Management.Smo.BackupDeviceItem(
$PRIVATE:deviceName,
[Microsoft.SqlServer.Management.Smo.DeviceType]$deviceType)
$PRIVATE:bkp.Devices.Add($PRIVATE:bkpItem)
}
if ($Overwrite) {
Write-Warning "Overwriting existing backup set"
}
$PRIVATE:bkp.SqlBackup($server)
if ($Differential) {
Write-Host -NoNewLine -F Blue "Differential Backup: "
}
Write-Host -NoNewLine "$($dbName) has been backed up to "
Write-Host -ForegroundColor Cyan -NoNewLine "$($deviceNames)"
Write-Host " successfully"
}
# BackupFiles.ps1
# Greg Lyon - July 2006
# reference: http://www.microsoft.com/technet/scriptcenter/csc/scripts/backup/backup/cscbk013.mspx
# Modified by DBMwS - July 14, 2006
param($strSourceFolder = "C:\programming\ps\test",
$strDestinationFolder = "F:\backup\programming\ps\test")
#{{{ function BackupFolder
function BackupFolder
{
param([string] $SourceDir = $(throw 'Enter Source Directory Name'),
[string] $DestinationDir = $(throw 'Enter Desitnation Directory Name'))
if (! (test-path $SourceDir)) {
throw "$($SourceDir) is not a valid directory!"
}
if (! (test-path $DestinationDir)) {
throw "$($DestinationDir) is not a valid directory!"
}
# Check if destination directory is missing any files in the source
# directory.if any file is missing, copy them over to the backup dir.
get-childitem $SourceDir |
foreach {
if ($_.PsIsContainer) {
$childDstDir = (join-path $DestinationDir $_.Name)
if (! (test-path $childDstDir)) { [void](mkdir $childDstDir) }
BackupFolder $_.FullName $childDstDir
} else {
$srcFile = $_.FullName
$destFile = join-path $DestinationDir $_.Name
if (test-path $destFile) {
$timeDiff = ($srcFile.LastWriteTime - $destFile.LastWriteTime).TotalSeconds
if ($timeDiff -gt 2) {
copy-item $srcFile $destFile -Force
write-host "Copied file $($srcFile) to $($destFile)"
$script:iCopyCount++
}
} else {
copy-item $srcFile $destFile -Force
write-host "Copied file $($srcFile) to $($destFile)"
$script:iCopyCount++
}
}
}
# Check if Source dir is missing any files on the backup dir.
# if there is/are file(s) missing, copy them over from backup dir to src dir
get-childitem $DestinationDir |
foreach {
if ($_.PsIsContainer) {
$childSrcDir = (join-path $SourceDir $_.Name)
if (! (test-path $childSrcDir)) { [void](mkdir $childSrcDir) }
BackupFolder $childSrcDir $_.FullName
} else {
$srcFile = join-path $SourceDir $_.Name
if (! (test-path $srcFile)) {
remove-item $_
write-host "Deleted file $($_)"
$script:iDestDeletedCount++
}
}
}
}
#}}}
#{{{ function WaitKey
function WaitKey {
param( [String] $strPrompt = "Press any key to continue ... ")
Write-Host
Write-Host $strPrompt -NoNewline
$key = [Console]::ReadKey($true)
Write-Host
}
#}}}
#{{{ MAIN BODY
$iCopyCount = 0
$iDestDeletedCount = 0
Write-Host
Write-Host "Backing up " -NoNewline -ForegroundColor "White"
Write-Host $strSourceFolder -ForegroundColor "Cyan" -NoNewline
Write-Host " to " -NoNewline -ForegroundColor "White"
Write-Host $strDestinationFolder -ForegroundColor "Cyan"
Write-Host
if (! (test-path $strSourceFolder)) {
Write-Host "Error: source folder does not exist!" -ForegroundColor "Red"
Write-Host
Write-Host "Exiting script"
WaitKey "Press any key to exit ... "
exit
}
if (! (test-path $strDestinationFolder)) {
Write-Host "Warning: destination folder`($($strDestinationFolder)`) does not exist"
$p = Read-Host "Create folder and continue? "
Write-Host
if ( $p[0] -ieq 'y' ) {
new-item -Type Directory -Path $strDestinationFolder | out-null
} else {
Write-Host "Exiting script"
WaitKey "Press any key to exit ... "
exit
}
}
BackupFolder $strSourceFolder $strDestinationFolder
if( ($iCopyCount -eq 0) -and ($iDestDeletedCount -eq 0) ) {
Write-Host
Write-Host "Folders are synchronized" -ForegroundColor "magenta"
} else {
Write-Host
Write-Host $iCopyCount "files copied from source to destination" -ForegroundColor "magenta"
Write-Host $iDestDeletedCount "orphaned destination files deleted" -ForegroundColor "magenta"
}
WaitKey "Press any key to exit ... "
#}}}
Experimenting with a different format of blogs...