Before running any scripts on a new powershell installation, you must first set an appropriate Execution Policy, e.g. Set-ExecutionPolicy RemoteSigned
There are two ways to run a PowerShell script.
The most common (default) way to run a script is by calling it:
PS C:\> & "C:\Belfry\My first Script.ps1"
If the path does not contain any spaces, then you can omit the quotes and the '&' operator
PS C:\> C:\Belfry\Myscript.ps1
If the script is in the current directory, you must indicate this using .\
PS C:\> .\Myscript.ps1
When you invoke a script using the syntax above, variables and functions defined in the script will disappear when the script ends.1
Dot Sourcing
When you dot source a script, all variables and functions defined in the script will persist even when the script ends.
Run a script by dot-sourcing it:
PS C:\> . "C:\Belfry\My first Script.ps1"
Dot-sourcing a script in the current directory:
PS C:\> . .\Myscript.ps1"
The System Path
If you run a script (or even just enter a command) without specifying the fully qualified path name, PowerShell will search for it as follows:
Firstly it will look at currently defined aliases, then currently defined functions and lastly commands located in the system path.
1unless they are explicitly defined as globals: Function SCOPE:GLOBAL or Filter SCOPE:GLOBAL or Set-Variable -scope "Global"
# ------------------------------------------------------------------------------
function writelog
{
param([string]$LogFile, [string]$data)
Write-Host $data
if ($LogFolder -ne "")
{
$data >> $LogFile
}else
{
}
}
# ------------------------------------------------------------------------------
function ZIPFolder
{
param( [string]$sourcefolder, [string]$outputfolder, [int]$retention, [string]$LogFolder )
$CompareDate=(Get-Date).AddDays(-$retention)
writelog $LogFolder ""
writelog $LogFolder "-- Search file old or equal this date: $CompareDate"
$a = Get-ChildItem -recurse $sourcefolder | where-object {$_.LastWriteTime -le $CompareDate}
foreach($x in $a)
{
writelog $LogFolder " evaluating file: $x"
#try/catch only Works in version 2
try
{
# $y = ((Get-Date) - $x.CreationTime).Days
$y = ((Get-Date) - $x.LastWriteTime).Days
# if ($y -gt $retention -and $x.PsISContainer -ne $True)
if ($x.PsISContainer -ne $True)
{
# $FileDate =Get-Date -format "dd-MMM-yyyy"
# $FileDate=($x.LastWriteTime).tostring("yyyy-mm-dd")
$FileDate=($x.LastWriteTime).tostring("yyyy-MM-dd")
$outputFile="$outputfolder$FileDate-output.zip"
writelog $LogFolder " Start ZIP file $x to package $outputFile"
$x |ZIPFile $outputFile
writelog $LogFolder " deleting file: $x"
# $x.delete()
writelog $LogFolder " Finished ZIP $x --> $outputFile, source file deleted"
writelog $LogFolder ""
}else
{
}
}
catch
{
writelog $LogFolder $_.Exception.ToString()
}
}
writelog $LogFolder "-- END Search file old or equal this date: $CompareDate"
}
# ------------------------------------------------------------------------------
function ZIPFile
{
param([string]$zipfilename)
if(-not (test-path($zipfilename)))
{
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
}
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
# $Path=$zipfilename.Remove($zipfilename.LastIndexOf("\")+1)
foreach($file in $input)
{
#write-host $zipPackage.Items().Contains($file)
#write-host $zipPackage.Items(0).tostring()
# if (-not (Test-Path("$zipfilename\$file")))
# {
$zipPackage.CopyHere($file.FullName,0x16)
Start-sleep -milliseconds 500
# }
}
}
# ------------------------------------------------------------------------------
1. Create a New Zip
function New-Zip
{
param([string]$zipfilename)
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
}usage: new-zip c:\demo\myzip.zip2. Add files to a zip via a pipeline
function Add-Zip
{
param([string]$zipfilename)
if(-not (test-path($zipfilename)))
{
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
}
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
foreach($file in $input)
{
$zipPackage.CopyHere($file.FullName)
Start-sleep -milliseconds 500
}
}usage: dir c:\demo\files\*.* -Recurse | add-Zip c:\demo\myzip.zip3. List the files in a zip
function Get-Zip
{
param([string]$zipfilename)
if(test-path($zipfilename))
{
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$zipPackage.Items() | Select Path
}
}usage: Get-Zip c:\demo\myzip.zip4. Extract the files form the zip
function Extract-Zip
{
param([string]$zipfilename, [string] $destination)
if(test-path($zipfilename))
{
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.CopyHere($zipPackage.Items())
}
}usage: extract-zip c:\demo\myzip.zip c:\demo\destination
So, how do we package the Vista Sidebar Gadget?
dir <path_to_gadget_files | add-Zip <path_to_gadget_zip> Rename-Item <path_to_gadget_zip> <path_to_gadget_zip>.Gadget
No comments:
Post a Comment