Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bunch of PSScriptAnalyzer warnings #35

Merged
merged 4 commits into from
Dec 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/BootStrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function NewBootStrap {
break;
}
catch {
Write-Error $_;
Write-Error -Message $_;
Start-Sleep -Seconds 5;
}
} #end while
Expand Down
2 changes: 1 addition & 1 deletion Lib/ConfigurationData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function ResolveConfigurationDataPath {
$resolvedPath = Join-Path -Path $labDefaults.ModuleRoot -ChildPath $configPath;
}
}
Write-Debug ('Resolved ''{0}'' configuration file to ''{1}''.' -f $Configuration, $resolvedPath);
Write-Debug -Message ('Resolved ''{0}'' configuration file to ''{1}''.' -f $Configuration, $resolvedPath);
return $resolvedPath;
} #end process
} #end function ReolveConfigurationPath
Expand Down
2 changes: 1 addition & 1 deletion Lib/DiskImage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function GetDiskImageDriveLetter {
process {
# Microsoft.Vhd.PowerShell.VirtualHardDisk
$driveLetter = Get-Partition -DiskNumber $DiskImage.DiskNumber |
Where Type -eq $PartitionType |
Where-Object Type -eq $PartitionType |
Where-Object DriveLetter |
Select-Object -Last 1 -ExpandProperty DriveLetter;
if (-not $driveLetter) {
Expand Down
8 changes: 4 additions & 4 deletions Lib/DscModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function ExpandDscModule {
)
process {
$targetPath = Join-Path -Path $DestinationPath -ChildPath $ModuleName;
if (-not (Test-Path $targetPath) -or $Force) {
if (-not (Test-Path -Path $targetPath) -or $Force) {
if (Test-Path -Path $targetPath) {
WriteVerbose ($localized.RemovingDirectory -f $targetPath);
Remove-Item -Path $targetPath -Recurse -Force -ErrorAction Stop;
Expand All @@ -22,7 +22,7 @@ function ExpandDscModule {
$archiveItems = $shellApplication.Namespace($Path).Items();
$shellApplication.NameSpace($DestinationPath).CopyHere($archiveItems);
## Rename any -master branch folder where no GitHub release available
Get-ChildItem -Path $DestinationPath -Directory | Where-Object { $_.Name -like '*-dev' -or $_.Name -like '*-master' } | % {
Get-ChildItem -Path $DestinationPath -Directory | Where-Object { $_.Name -like '*-dev' -or $_.Name -like '*-master' } | ForEach-Object {
$destinationFilename = $_.Name -replace '-dev','' -replace '-master','';
WriteVerbose ($localized.RenamingPath -f $_.FullName, $destinationFilename);
Rename-Item -Path $_.FullName -NewName $destinationFilename -ErrorAction Stop;
Expand Down Expand Up @@ -69,12 +69,12 @@ function GetDscModule {
$dscModulePath = Join-Path -Path $dscModulePath -ChildPath "DSCResources\$ResourceName";
}
if (-not (Test-Path -Path $dscModulePath)) {
Write-Error ($localized.DscResourceNotFoundError -f $ModuleName);
Write-Error -Message ($localized.DscResourceNotFoundError -f $ModuleName);
return $null;
}
if ($MinimumVersion) {
if ($Module.Version -lt [System.Version]$MinimumVersion) {
Write-Error ($localized.ResourceVersionMismatchError -f $ModuleName, $module.Version.ToString(), $MinimumVersion);
Write-Error -Message ($localized.ResourceVersionMismatchError -f $ModuleName, $module.Version.ToString(), $MinimumVersion);
return $null;
}
}
Expand Down
6 changes: 3 additions & 3 deletions Lib/DscResource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function ImportDscResource {
}
}
else {
Write-Debug ($localized.DscResourceAlreadyImported -f $ModuleName, $ResourceName);
Write-Debug -Message ($localized.DscResourceAlreadyImported -f $ModuleName, $ResourceName);
}
} #end process
} #end function ImportDscResource
Expand Down Expand Up @@ -76,7 +76,7 @@ function TestDscResource {
$testTargetResourceCommand = 'Test-{0}TargetResource' -f $ResourceName;
WriteVerbose ($localized.InvokingCommand -f $testTargetResourceCommand);
$Parameters.Keys | ForEach-Object {
Write-Debug ($localized.CommandParameter -f $_, $Parameters.$_);
Write-Debug -Message ($localized.CommandParameter -f $_, $Parameters.$_);
}
$testDscResourceResult = & $testTargetResourceCommand @Parameters;
if (-not $testDscResourceResult) {
Expand All @@ -103,7 +103,7 @@ function SetDscResource {
$setTargetResourceCommand = 'Set-{0}TargetResource' -f $ResourceName;
WriteVerbose ($localized.InvokingCommand -f $setTargetResourceCommand);
$Parameters.Keys | ForEach-Object {
Write-Debug ($localized.CommandParameter -f $_, $Parameters.$_);
Write-Debug -Message ($localized.CommandParameter -f $_, $Parameters.$_);
}
return (& $setTargetResourceCommand @Parameters);
} #end process
Expand Down
20 changes: 10 additions & 10 deletions Lib/DscResourceModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ function TestDscResourceModule {
process {
## This module contains a \DSCResources folder, but we don't want to enumerate this!
if ($Path -notmatch "\\$($labDefaults.ModuleName)$") {
Write-Debug ('Testing for MOF-based DSC Resource ''{0}'' directory.' -f "$Path\DSCResources");
Write-Debug -Message ('Testing for MOF-based DSC Resource ''{0}'' directory.' -f "$Path\DSCResources");
if (Test-Path -Path "$Path\DSCResources" -PathType Container) {
## We have a WMF 4.0/MOF DSC resource module
Write-Debug ('Found MOF-based DSC resource ''{0}''.' -f $Path);
Write-Debug -Message ('Found MOF-based DSC resource ''{0}''.' -f $Path);
return $true;
}

Write-Debug ('Testing for Class-based DSC resource definition ''{0}''.' -f "$Path\$ModuleName.psm1");
Write-Debug -Message ('Testing for Class-based DSC resource definition ''{0}''.' -f "$Path\$ModuleName.psm1");
if (Test-Path -Path "$Path\$ModuleName.psm1") {
$psm1Content = Get-Content -Path "$Path\$ModuleName.psm1";
## If there's a .psm1 file, check if it's a class-based DSC resource
if ($psm1Content -imatch '^(\s*)\[DscResource\(\)\](\s*)$') {
## File has a [DscResource()] declaration
Write-Debug ('Found Class-based DSC resource ''{0}''.' -f $Path);
Write-Debug -Message ('Found Class-based DSC resource ''{0}''.' -f $Path);
return $true;
}
}
Expand Down Expand Up @@ -60,9 +60,9 @@ function GetDscResourceModule {
$moduleInfo = $PSItem;
## Check to see if we have a MOF or class resource in the module
if (TestDscResourceModule -Path $moduleInfo.FullName -ModuleName $moduleInfo.Name) {
Write-Debug ('Discovered DSC resource ''{0}''.' -f $moduleInfo.FullName);
Write-Debug -Message ('Discovered DSC resource ''{0}''.' -f $moduleInfo.FullName);
$module = Test-ModuleManifest -Path "$($moduleInfo.FullName)\$($moduleInfo.Name).psd1";
Write-Output ([PSCustomObject] @{
Write-Output -InputObject ([PSCustomObject] @{
ModuleName = $moduleInfo.Name;
ModuleVersion = [System.Version] $module.Version;
Path = $moduleInfo.FullName;
Expand All @@ -71,13 +71,13 @@ function GetDscResourceModule {
else {
## Enumerate each module\<number>.<number> subdirectory
Get-ChildItem -Path $moduleInfo.FullName -Directory | Where-Object Name -match '^\d+\.\d+' | ForEach-Object {
Write-Debug ('Checking module versioned directory ''{0}''.' -f $PSItem.FullName);
Write-Debug -Message ('Checking module versioned directory ''{0}''.' -f $PSItem.FullName);
## Test to see if it's a DSC resource module
if (TestDscResourceModule -Path $PSItem.FullName -ModuleName $moduleInfo.Name) {
try {
#$moduleVersion = [System.Version] $PSItem.Name;
Write-Debug ('Discovered versioned DSC resource ''{0}''.' -f $PSItem.FullName);
Write-Output ([PSCustomObject] @{
Write-Debug -Message ('Discovered versioned DSC resource ''{0}''.' -f $PSItem.FullName);
Write-Output -InputObject ([PSCustomObject] @{
ModuleName = $moduleInfo.Name;
ModuleVersion = [System.Version] $PSItem.Name;
Path = "$($moduleInfo.FullName)\$($PSItem.Name)";
Expand All @@ -86,7 +86,7 @@ function GetDscResourceModule {
catch { }
}
} | #end foreach module\<number>.<number> sub directory
Sort-Object -Property ModuleVersion -Descending | Select -First 1;
Sort-Object -Property ModuleVersion -Descending | Select-Object -First 1;
}
} #end foreach module directory
} #end foreach path
Expand Down
8 changes: 4 additions & 4 deletions Lib/Internal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ function InvokeExecutable {
NoNewWindow = $true;
PassThru = $true;
}
Write-Debug ($localized.RedirectingOutput -f 'StdOut', $processArgs.RedirectStandardOutput);
Write-Debug ($localized.RedirectingOutput -f 'StdErr', $processArgs.RedirectStandardError);
Write-Debug -Message ($localized.RedirectingOutput -f 'StdOut', $processArgs.RedirectStandardOutput);
Write-Debug -Message ($localized.RedirectingOutput -f 'StdErr', $processArgs.RedirectStandardError);
WriteVerbose ($localized.StartingProcess -f $Path, [System.String]::Join(' ', $Arguments));
$process = Start-Process @processArgs;
if ($process.ExitCode -ne 0) { WriteWarning ($localized.ProcessExitCode -f $Path, $process.ExitCode);}
Expand All @@ -70,7 +70,7 @@ function WriteVerbose {
[Parameter(Mandatory, ValueFromPipeline)] [System.String] $Message
)
process {
Write-Verbose ('[{0}] {1}' -f (Get-Date).ToLongTimeString(), $Message);
Write-Verbose -Message ('[{0}] {1}' -f (Get-Date).ToLongTimeString(), $Message);
}
}

Expand All @@ -84,6 +84,6 @@ function WriteWarning {
[Parameter(Mandatory, ValueFromPipeline)] [System.String] $Message
)
process {
Write-Warning ('[{0}] {1}' -f (Get-Date).ToLongTimeString(), $Message);
Write-Warning -Message ('[{0}] {1}' -f (Get-Date).ToLongTimeString(), $Message);
}
}
16 changes: 8 additions & 8 deletions Lib/Resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ function NewDirectory {
[ValidateNotNullOrEmpty()] [System.String[]] $Path
)
process {
Write-Debug ("Using parameter set '{0}'." -f $PSCmdlet.ParameterSetName);
Write-Debug -Message ("Using parameter set '{0}'." -f $PSCmdlet.ParameterSetName);
switch ($PSCmdlet.ParameterSetName) {
'ByString' {
foreach ($directory in $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)) {
Write-Debug ("Testing target directory '{0}'." -f $directory);
if (!(Test-Path $directory -PathType Container)) {
Write-Debug -Message ("Testing target directory '{0}'." -f $directory);
if (!(Test-Path -Path $directory -PathType Container)) {
if ($PSCmdlet.ShouldProcess($directory, "Create directory")) {
WriteVerbose ($localized.CreatingDirectory -f $directory);
New-Item -Path $directory -ItemType Directory;
Expand All @@ -39,7 +39,7 @@ function NewDirectory {

'ByDirectoryInfo' {
foreach ($directoryInfo in $InputObject) {
Write-Debug ("Testing target directory '{0}'." -f $directoryInfo.FullName);
Write-Debug -Message ("Testing target directory '{0}'." -f $directoryInfo.FullName);
if (!($directoryInfo.Exists)) {
if ($PSCmdlet.ShouldProcess($directoryInfo.FullName, "Create directory")) {
WriteVerbose ($localized.CreatingDirectory -f $directoryInfo.FullName);
Expand Down Expand Up @@ -99,12 +99,12 @@ function GetResourceDownload {
[ref] $null = SetResourceChecksum -Path $DestinationPath;
}
if (Test-Path -Path $checksumPath) {
Write-Debug ('MD5 checksum file ''{0}'' found.' -f $checksumPath);
Write-Debug -Message ('MD5 checksum file ''{0}'' found.' -f $checksumPath);
$md5Checksum = (Get-Content -Path $checksumPath -Raw).Trim();
Write-Debug ('Discovered MD5 checksum ''{0}''.' -f $md5Checksum);
Write-Debug -Message ('Discovered MD5 checksum ''{0}''.' -f $md5Checksum);
}
else {
Write-Debug ('MD5 checksum file ''{0}'' not found.' -f $checksumPath);
Write-Debug -Message ('MD5 checksum file ''{0}'' not found.' -f $checksumPath);
}
$resource = @{
DestinationPath = $DestinationPath;
Expand Down Expand Up @@ -211,7 +211,7 @@ function InvokeWebClientDownload {
[System.UInt64] $contentLength = $webClient.ResponseHeaders['Content-Length'];
$path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($DestinationPath);
[System.IO.Stream] $outputStream = [System.IO.File]::Create($path);
[System.Byte[]] $buffer = New-Object System.Byte[] $BufferSize;
[System.Byte[]] $buffer = New-Object -TypeName System.Byte[] -ArgumentList $BufferSize;
[System.UInt64] $bytesRead = 0;
[System.UInt64] $totalBytes = 0;
do {
Expand Down
2 changes: 1 addition & 1 deletion Lib/UnattendXml.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function NewUnattendXml {
}
} #end foreach setting.Component
} #end foreach unattendXml.Unattend.Settings
Write-Output $unattendXml;
Write-Output -InputObject $unattendXml;
} #end process
} #end function NewUnattendXml

Expand Down
14 changes: 7 additions & 7 deletions Lib/ZipArchive.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function ExpandZipArchive {
}
}
## If all tests passed, load the required .NET assemblies
Write-Debug 'Loading ''System.IO.Compression'' .NET binaries.';
Write-Debug -Message 'Loading ''System.IO.Compression'' .NET binaries.';
Add-Type -AssemblyName 'System.IO.Compression';
Add-Type -AssemblyName 'System.IO.Compression.FileSystem';
} # end begin
Expand All @@ -55,7 +55,7 @@ function ExpandZipArchive {
ExpandZipArchiveItem @expandZipArchiveItemParams;
} # end try
catch {
Write-Error $_.Exception;
Write-Error -Message $_.Exception;
}
finally {
## Close the file handle
Expand Down Expand Up @@ -91,7 +91,7 @@ function ExpandZipArchiveItem {
[System.Management.Automation.SwitchParameter] $Force
)
begin {
Write-Debug 'Loading ''System.IO.Compression'' .NET binaries.';
Write-Debug -Message 'Loading ''System.IO.Compression'' .NET binaries.';
Add-Type -AssemblyName 'System.IO.Compression';
Add-Type -AssemblyName 'System.IO.Compression.FileSystem';
}
Expand All @@ -102,7 +102,7 @@ function ExpandZipArchiveItem {
if ($zipArchiveEntry.FullName.Contains('/')) {
## We need to create the directory path as the ExtractToFile extension method won't do this and will throw an exception
$pathSplit = $zipArchiveEntry.FullName.Split('/');
$relativeDirectoryPath = New-Object System.Text.StringBuilder;
$relativeDirectoryPath = New-Object -TypeName System.Text.StringBuilder;

## Generate the relative directory name
for ($pathSplitPart = 0; $pathSplitPart -lt ($pathSplit.Count -1); $pathSplitPart++) {
Expand All @@ -126,7 +126,7 @@ function ExpandZipArchiveItem {
## This is a folder and we need to create the directory path as the
## ExtractToFile extension method won't do this and will throw an exception
$pathSplit = $zipArchiveEntry.FullName.Split('/');
$relativeDirectoryPath = New-Object System.Text.StringBuilder;
$relativeDirectoryPath = New-Object -TypeName System.Text.StringBuilder;

## Generate the relative directory name
for ($pathSplitPart = 0; $pathSplitPart -lt ($pathSplit.Count -1); $pathSplitPart++) {
Expand All @@ -152,13 +152,13 @@ function ExpandZipArchiveItem {
WriteVerbose ($localized.ExtractingZipArchiveEntry -f $fullDestinationFilePath);
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($zipArchiveEntry, $fullDestinationFilePath, $true);
## Return a FileInfo object to the pipline
Write-Output (Get-Item -Path $fullDestinationFilePath);
Write-Output -InputObject (Get-Item -Path $fullDestinationFilePath);
}
} # end if
} # end foreach zipArchiveEntry
} # end try
catch {
Write-Error $_.Exception;
Write-Error -Message $_.Exception;
}
} # end process
} #end function ExpandZipArchiveItem
Expand Down
2 changes: 1 addition & 1 deletion Resources.psd1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ConvertFrom-StringData @'
ConvertFrom-StringData -StringData @'
DownloadingResource = Downloading resource '{0}' to '{1}'.
DownloadingActivity = Downloading '{0}'.
DownloadStatus = {0:N0} of {1:N0} bytes ({2} %).
Expand Down
4 changes: 2 additions & 2 deletions Src/Lab.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function Checkpoint-Lab {
}
elseif ($runningNodes) {
foreach ($runningNode in $runningNodes) {
Write-Error ($localized.CannotSnapshotNodeError -f $runningNode.Name);
Write-Error -Message ($localized.CannotSnapshotNodeError -f $runningNode.Name);
}
}
else {
Expand Down Expand Up @@ -268,7 +268,7 @@ function Restore-Lab {
}
elseif ($runningNodes) {
foreach ($runningNode in $runningNodes) {
Write-Error ($localized.CannotSnapshotNodeError -f $runningNode.NodeName);
Write-Error -Message ($localized.CannotSnapshotNodeError -f $runningNode.NodeName);
}
}
else {
Expand Down
8 changes: 4 additions & 4 deletions Src/LabConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Test-LabConfiguration {
}
process {
WriteVerbose $localized.StartedLabConfigurationTest;
$nodes = $ConfigurationData.AllNodes | Where { $_.NodeName -ne '*' };
$nodes = $ConfigurationData.AllNodes | Where-Object { $_.NodeName -ne '*' };
foreach ($node in $nodes) {
[PSCustomObject] @{
Name = $node.NodeName;
Expand Down Expand Up @@ -61,7 +61,7 @@ function TestLabConfigurationMof {
}
process {
$Path = Resolve-Path -Path $Path -ErrorAction Stop;
$node = $ConfigurationData.AllNodes | Where { $_.NodeName -eq $Name };
$node = $ConfigurationData.AllNodes | Where-Object { $_.NodeName -eq $Name };

$mofPath = Join-Path -Path $Path -ChildPath ('{0}.mof' -f $node.NodeName);
WriteVerbose ($localized.CheckingForNodeFile -f $mofPath);
Expand Down Expand Up @@ -200,7 +200,7 @@ function Start-LabConfiguration {
}
process {
WriteVerbose $localized.StartedLabConfiguration;
$nodes = $ConfigurationData.AllNodes | Where { $_.NodeName -ne '*' };
$nodes = $ConfigurationData.AllNodes | Where-Object { $_.NodeName -ne '*' };

$Path = ResolvePathEx -Path $Path;
foreach ($node in $nodes) {
Expand Down Expand Up @@ -266,7 +266,7 @@ function Remove-LabConfiguration {
}
process {
WriteVerbose $localized.StartedLabConfiguration;
$nodes = $ConfigurationData.AllNodes | Where { $_.NodeName -ne '*' };
$nodes = $ConfigurationData.AllNodes | Where-Object { $_.NodeName -ne '*' };
foreach ($node in $nodes) {
##TODO: Should this not ensure that VMs are powered off?
RemoveLabVM -Name $node.NodeName -ConfigurationData $ConfigurationData -RemoveSwitch:$RemoveSwitch;
Expand Down
Loading