diff --git a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Compress-Folder.ps1 b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Compress-Folder.ps1 index e5a78819ad..996f27c1cb 100644 --- a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Compress-Folder.ps1 +++ b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Compress-Folder.ps1 @@ -11,94 +11,65 @@ Function Compress-Folder { [Parameter(Position = 4)][bool]$ReturnCompressedLocation = $false ) - Function Get-DirectorySize { - param( - [Parameter(Mandatory = $true)][string]$Directory, - [Parameter(Mandatory = $false)][bool]$IsCompressed = $false - ) - $itemSize = 0 - if ($IsCompressed) { - $itemSize = (Get-Item $Directory).Length - } else { - $childItems = Get-ChildItem $Directory -Recurse | Where-Object { -not($_.Mode.StartsWith("d-")) } - foreach ($item in $childItems) { - $itemSize += $item.Length - } - } - return $itemSize - } - - if ($Folder.EndsWith("\")) { - $Folder = $Folder.TrimEnd("\") - } + $Folder = $Folder.TrimEnd("\") + $compressedLocation = [string]::Empty Write-Verbose "Calling: $($MyInvocation.MyCommand)" Write-Verbose "Passed - [string]Folder: $Folder | [bool]IncludeDisplayZipping: $IncludeDisplayZipping | [bool]ReturnCompressedLocation: $ReturnCompressedLocation" - $compressedLocation = [string]::Empty - if (Test-Path $Folder) { + if (-not (Test-Path $Folder)) { + Write-Host "Failed to find the folder $Folder" + return $null + } + + $successful = ([Appdomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.Location -like "*System.IO.Compression.Filesystem*" }).Count -ge 1 + Write-Verbose "Found IO Compression loaded: $successful" - $assemblies = [Appdomain]::CurrentDomain.GetAssemblies() - $successful = $false - foreach ($assembly in $assemblies) { - if ($assembly.Location -like "*System.IO.Compression.Filesystem*") { - $successful = $true - break - } + if ($successful -eq $false) { + # Try to load the IO Compression + try { + Add-Type -AssemblyName System.IO.Compression.Filesystem -ErrorAction Stop + Write-Verbose "Loaded .NET Compression Assembly." + } catch { + Write-Host "Failed to load .NET Compression assembly. Unable to compress up the data." + return $null } + } - Write-Verbose "Found IO Compression loaded: $successful" + if ($IncludeMonthDay) { + $zipFolderNoEXT = "{0}-{1}" -f $Folder, (Get-Date -Format Md) + } else { + $zipFolderNoEXT = $Folder + } + Write-Verbose "[string]zipFolderNoEXT: $zipFolderNoEXT" + $zipFolder = "{0}.zip" -f $zipFolderNoEXT + [int]$i = 1 + while (Test-Path $zipFolder) { + $zipFolder = "{0}-{1}.zip" -f $zipFolderNoEXT, $i + $i++ + } + Write-Verbose "Using Zip Folder Path: $zipFolder" - if ($successful -eq $false) { - # Try to load the IO Compression - $loadedIOCompression = $false - try { - Add-Type -AssemblyName System.IO.Compression.Filesystem -ErrorAction Stop - $loadedIOCompression = $true - } catch { - Write-Host "Failed to load .NET Compression assembly. Unable to compress up the data." - } + if ($IncludeDisplayZipping) { + Write-Host "Compressing Folder $Folder" + } + $sizeBytesBefore = 0 + Get-ChildItem $Folder -Recurse | + Where-Object { -not ($_.Mode.StartsWith("d-")) } | + ForEach-Object { $sizeBytesBefore += $_.Length } - if ($loadedIOCompression -eq $false) { - Write-Verbose "Unable to compress folder $Folder" - Write-Verbose "Unable to enable IO compression on this system" - return $null - } - } + $timer = [System.Diagnostics.Stopwatch]::StartNew() + [System.IO.Compression.ZipFile]::CreateFromDirectory($Folder, $zipFolder) + $timer.Stop() + $sizeBytesAfter = (Get-Item $zipFolder).Length + Write-Verbose ("Compressing directory size of {0} MB down to the size of {1} MB took {2} seconds." -f ($sizeBytesBefore / 1MB), ($sizeBytesAfter / 1MB), $timer.Elapsed.TotalSeconds) - if ($IncludeMonthDay) { - $zipFolderNoEXT = "{0}-{1}" -f $Folder, (Get-Date -Format Md) - } else { - $zipFolderNoEXT = $Folder - } - Write-Verbose "[string]zipFolderNoEXT: $zipFolderNoEXT" - $zipFolder = "{0}.zip" -f $zipFolderNoEXT - if (Test-Path $zipFolder) { - [int]$i = 1 - do { - $zipFolder = "{0}-{1}.zip" -f $zipFolderNoEXT, $i - $i++ - }while (Test-Path $zipFolder) - } - Write-Verbose "Using Zip Folder Path: $zipFolder" + if ((Test-Path -Path $zipFolder)) { + Write-Verbose "Compress successful, removing folder." + Remove-Item $Folder -Force -Recurse + } - if ($IncludeDisplayZipping) { - Write-Host "Compressing Folder $Folder" - } - $sizeBytesBefore = Get-DirectorySize -Directory $Folder - $timer = [System.Diagnostics.Stopwatch]::StartNew() - [System.IO.Compression.ZipFile]::CreateFromDirectory($Folder, $zipFolder) - $timer.Stop() - $sizeBytesAfter = Get-DirectorySize -Directory $zipFolder -IsCompressed $true - Write-Verbose ("Compressing directory size of {0} MB down to the size of {1} MB took {2} seconds." -f ($sizeBytesBefore / 1MB), ($sizeBytesAfter / 1MB), $timer.Elapsed.TotalSeconds) - if ((Test-Path -Path $zipFolder)) { - Write-Verbose "Compress successful, removing folder." - Remove-Item $Folder -Force -Recurse - } - if ($ReturnCompressedLocation) { - $compressedLocation = $zipFolder - } - } else { - Write-Host "Failed to find the folder $Folder" + if ($ReturnCompressedLocation) { + $compressedLocation = $zipFolder } Write-Verbose "Returning: $compressedLocation" diff --git a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-BulkItems.ps1 b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-BulkItems.ps1 index 8cc3079425..ec9b41cda6 100644 --- a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-BulkItems.ps1 +++ b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-BulkItems.ps1 @@ -1,7 +1,6 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. -. $PSScriptRoot\New-Folder.ps1 . $PSScriptRoot\..\Get-StringDataForNotEnoughFreeSpace.ps1 . $PSScriptRoot\..\Test-FreeSpace.ps1 Function Copy-BulkItems { @@ -9,9 +8,8 @@ Function Copy-BulkItems { [string]$CopyToLocation, [Array]$ItemsToCopyLocation ) - if (-not(Test-Path $CopyToLocation)) { - New-Folder -NewFolder $CopyToLocation -IncludeDisplayCreate $true - } + + New-Item -ItemType Directory -Path $CopyToLocation -Force | Out-Null if (Test-FreeSpace -FilePaths $ItemsToCopyLocation) { foreach ($item in $ItemsToCopyLocation) { diff --git a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-FullLogFullPathRecurse.ps1 b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-FullLogFullPathRecurse.ps1 index 4893ce07fd..73df0ec986 100644 --- a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-FullLogFullPathRecurse.ps1 +++ b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-FullLogFullPathRecurse.ps1 @@ -1,7 +1,6 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. -. $PSScriptRoot\New-Folder.ps1 . $PSScriptRoot\..\Get-StringDataForNotEnoughFreeSpace.ps1 . $PSScriptRoot\..\Test-FreeSpace.ps1 Function Copy-FullLogFullPathRecurse { @@ -11,7 +10,7 @@ Function Copy-FullLogFullPathRecurse { ) Write-Verbose("Function Enter: Copy-FullLogFullPathRecurse") Write-Verbose("Passed: [string]LogPath: {0} | [string]CopyToThisLocation: {1}" -f $LogPath, $CopyToThisLocation) - New-Folder -NewFolder $CopyToThisLocation -IncludeDisplayCreate $true + New-Item -ItemType Directory -Path $CopyToThisLocation -Force | Out-Null if (Test-Path $LogPath) { $childItems = Get-ChildItem $LogPath -Recurse $items = @() diff --git a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-LogmanData.ps1 b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-LogmanData.ps1 index 60ea4506e9..bc629a891d 100644 --- a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-LogmanData.ps1 +++ b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-LogmanData.ps1 @@ -2,7 +2,6 @@ # Licensed under the MIT License. . $PSScriptRoot\Copy-BulkItems.ps1 -. $PSScriptRoot\New-Folder.ps1 Function Copy-LogmanData { param( [Parameter(Mandatory = $true)]$ObjLogman @@ -18,7 +17,7 @@ Function Copy-LogmanData { $strDirectory = $ObjLogman.RootPath $copyTo = $Script:RootCopyToDirectory + "\" + $folderName - New-Folder -NewFolder $copyTo -IncludeDisplayCreate $true + New-Item -ItemType Directory -Path $copyTo -Force | Out-Null if (Test-Path $strDirectory) { $wildExt = "*" + $objLogman.Ext $filterDate = $objLogman.StartDate diff --git a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-LogsBasedOnTime.ps1 b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-LogsBasedOnTime.ps1 index 7e6fba00f4..141d2ee258 100644 --- a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-LogsBasedOnTime.ps1 +++ b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Copy-LogsBasedOnTime.ps1 @@ -2,7 +2,6 @@ # Licensed under the MIT License. . $PSScriptRoot\Copy-BulkItems.ps1 -. $PSScriptRoot\New-Folder.ps1 Function Copy-LogsBasedOnTime { param( [Parameter(Mandatory = $false)][string]$LogPath, @@ -16,7 +15,7 @@ Function Copy-LogsBasedOnTime { return } - New-Folder -NewFolder $CopyToThisLocation -IncludeDisplayCreate $true + New-Item -ItemType Directory -Path $CopyToThisLocation -Force | Out-Null Function NoFilesInLocation { param( @@ -66,7 +65,7 @@ Function Copy-LogsBasedOnTime { foreach ($dir in $directories) { $newLogPath = $dir.FullName $newCopyToThisLocation = "{0}\{1}" -f $CopyToThisLocation, $dir.Name - New-Folder -NewFolder $newCopyToThisLocation -IncludeDisplayCreate $true + New-Item -ItemType Directory -Path $newCopyToThisLocation -Force | Out-Null $files = Get-ChildItem $newLogPath | Sort-Object LastWriteTime -Descending | Where-Object { $_.LastWriteTime -ge $copyFromDate -and $_.Mode -notlike "d*" } if ($null -eq $files) { diff --git a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/New-Folder.ps1 b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/New-Folder.ps1 deleted file mode 100644 index 496839fe7a..0000000000 --- a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/New-Folder.ps1 +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. - -Function New-Folder { - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Caller knows that this is an action')] - [CmdletBinding()] - param( - [Alias("NewFolder")] - [Parameter(Mandatory = $false)][array]$NewFolders, - [Parameter(Mandatory = $false)][bool]$IncludeDisplayCreate - ) - - if ($NewFolders.Count -gt 1) { - $verboseDisplayNewFolders = "Multiple ('{0}') Folders Passed" -f $NewFolders.Count - } else { - $verboseDisplayNewFolders = $NewFolders[0] - } - Write-Verbose "Calling: $($MyInvocation.MyCommand)" - Write-Verbose "Passed: [string]NewFolders: $verboseDisplayNewFolders | [bool]IncludeDisplayCreate: $IncludeDisplayCreate" - - foreach ($newFolder in $NewFolders) { - if (-not (Test-Path -Path $newFolder)) { - if ($IncludeDisplayCreate) { - Write-Host "Creating Directory: $newFolder" - } - [System.IO.Directory]::CreateDirectory($newFolder) | Out-Null - } else { - if ($IncludeDisplayCreate) { - Write-Host "Directory $newFolder is already created!" - } - } - } -} diff --git a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Save-FailoverClusterInformation.ps1 b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Save-FailoverClusterInformation.ps1 index 62b99b15d8..ab6f1a385b 100644 --- a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Save-FailoverClusterInformation.ps1 +++ b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Save-FailoverClusterInformation.ps1 @@ -1,14 +1,13 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. -. $PSScriptRoot\New-Folder.ps1 . $PSScriptRoot\Save-DataInfoToFile.ps1 . $PSScriptRoot\..\Get-ClusterNodeFileVersions.ps1 #Save out the failover cluster information for the local node, besides the event logs. Function Save-FailoverClusterInformation { Write-Verbose("Function Enter: Save-FailoverClusterInformation") $copyTo = "$Script:RootCopyToDirectory\Cluster_Information" - New-Folder -NewFolder $copyTo -IncludeDisplayCreate $true + New-Item -ItemType Directory -Path $copyTo -Force | Out-Null try { Save-DataInfoToFile -DataIn (Get-Cluster -ErrorAction Stop) -SaveToLocation "$copyTo\GetCluster" diff --git a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Save-ServerInfoData.ps1 b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Save-ServerInfoData.ps1 index 20f9f563d3..609f309c34 100644 --- a/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Save-ServerInfoData.ps1 +++ b/Diagnostics/ExchangeLogCollector/RemoteScriptBlock/IO/Save-ServerInfoData.ps1 @@ -1,14 +1,13 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. -. $PSScriptRoot\New-Folder.ps1 . $PSScriptRoot\Save-DataInfoToFile.ps1 . $PSScriptRoot\..\Add-ServerNameToFileName.ps1 . $PSScriptRoot\..\Test-CommandExists.ps1 Function Save-ServerInfoData { Write-Verbose("Function Enter: Save-ServerInfoData") $copyTo = $Script:RootCopyToDirectory + "\General_Server_Info" - New-Folder -NewFolder $copyTo -IncludeDisplayCreate $true + New-Item -ItemType Directory -Path $copyTo -Force | Out-Null #Get MSInfo from server msinfo32.exe /nfo (Add-ServerNameToFileName -FilePath ("{0}\msinfo.nfo" -f $copyTo)) diff --git a/Diagnostics/ExchangeLogCollector/Write/Write-DataOnlyOnceOnMasterServer.ps1 b/Diagnostics/ExchangeLogCollector/Write/Write-DataOnlyOnceOnMasterServer.ps1 index 0ce9abf8c1..82896a5704 100644 --- a/Diagnostics/ExchangeLogCollector/Write/Write-DataOnlyOnceOnMasterServer.ps1 +++ b/Diagnostics/ExchangeLogCollector/Write/Write-DataOnlyOnceOnMasterServer.ps1 @@ -2,7 +2,6 @@ # Licensed under the MIT License. . $PSScriptRoot\..\ExchangeServerInfo\Get-VirtualDirectoriesLdap.ps1 -. $PSScriptRoot\..\RemoteScriptBlock\IO\New-Folder.ps1 . $PSScriptRoot\..\RemoteScriptBlock\IO\Save-DataInfoToFile.ps1 Function Write-DataOnlyOnceOnMasterServer { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseUsingScopeModifierInNewRunspaces', '', Justification = 'Can not use using for an env variable')] @@ -31,7 +30,7 @@ Function Write-DataOnlyOnceOnMasterServer { if ($SendConnectors) { $create = $RootCopyToDirectory + "\Connectors" - New-Folder -NewFolder $create -IncludeDisplayCreate $true + New-Item -ItemType Directory -Path $create -Force | Out-Null $saveLocation = $create + "\Send_Connectors" Save-DataInfoToFile -dataIn (Get-SendConnector) -SaveToLocation $saveLocation -AddServerName $false } diff --git a/Diagnostics/ExchangeLogCollector/Write/Write-LargeDataObjectsOnMachine.ps1 b/Diagnostics/ExchangeLogCollector/Write/Write-LargeDataObjectsOnMachine.ps1 index 37379c34ce..cd351a11bd 100644 --- a/Diagnostics/ExchangeLogCollector/Write/Write-LargeDataObjectsOnMachine.ps1 +++ b/Diagnostics/ExchangeLogCollector/Write/Write-LargeDataObjectsOnMachine.ps1 @@ -9,7 +9,6 @@ . $PSScriptRoot\..\RemoteScriptBlock\Get-ExchangeInstallDirectory.ps1 . $PSScriptRoot\..\RemoteScriptBlock\IO\Compress-Folder.ps1 . $PSScriptRoot\..\RemoteScriptBlock\IO\Invoke-CatchBlockActions.ps1 -. $PSScriptRoot\..\RemoteScriptBlock\IO\New-Folder.ps1 . $PSScriptRoot\..\RemoteScriptBlock\IO\Save-DataToFile.ps1 #This function job is to write out the Data that is too large to pass into the main script block #This is for mostly Exchange Related objects. @@ -184,7 +183,7 @@ Function Write-LargeDataObjectsOnMachine { $rootSaveToLocation = $DAGWriteInfo.RootSaveToLocation $mailboxDatabaseSaveToLocation = "{0}\MailboxDatabase\" -f $rootSaveToLocation $copyStatusSaveToLocation = "{0}\MailboxDatabaseCopyStatus\" -f $rootSaveToLocation - New-Folder -NewFolders @($mailboxDatabaseSaveToLocation, $copyStatusSaveToLocation) + New-Item -ItemType Directory -Path @($mailboxDatabaseSaveToLocation, $copyStatusSaveToLocation) -Force | Out-Null Save-DataToFile -DataIn $DAGWriteInfo.DAGInfo -SaveToLocation ("{0}{1}_DatabaseAvailabilityGroup" -f $rootSaveToLocation, $dagName) Save-DataToFile -DataIn $DAGWriteInfo.DAGNetworkInfo -SaveToLocation ("{0}{1}_DatabaseAvailabilityGroupNetwork" -f $rootSaveToLocation, $dagName) Save-DataToFile -DataIn $DAGWriteInfo.MailboxDatabaseCopyStatusServer -SaveToLocation ("{0}{1}_MailboxDatabaseCopyStatus" -f $copyStatusSaveToLocation, $serverName) @@ -237,7 +236,7 @@ Function Write-LargeDataObjectsOnMachine { Write-Verbose("Remote Copy Location: $remoteLocation") $rootTempLocation = "{0}{1}\{2}_Exchange_DAG_Information\" -f $localServerTempLocation, $_.ServerName, $_.DAGInfo.Name Write-Verbose("Local Root Temp Location: $rootTempLocation") - New-Folder -NewFolders $rootTempLocation + New-Item -ItemType Directory -Path $rootTempLocation -Force | Out-Null $_ | Add-Member -MemberType NoteProperty -Name RootSaveToLocation -Value $rootTempLocation Write-DatabaseAvailabilityGroupDataLocal -DAGWriteInfo $_ @@ -267,7 +266,7 @@ Function Write-LargeDataObjectsOnMachine { ForEach-Object { $failed = $false $reportPath = "{0}\{1}_FailoverMetrics" -f $localServerTempLocation, $_.Name - New-Folder -NewFolders $reportPath + New-Item -ItemType Directory -Path $reportPath -Force | Out-Null try { Write-Host "Attempting to run CollectOverMetrics.ps1 against $($_.Name)" @@ -381,12 +380,8 @@ Function Write-LargeDataObjectsOnMachine { Write-Verbose("Creating Script Block") $getExchangeInstallDirectoryScriptBlock = [scriptblock]::Create($getExchangeInstallDirectoryString) - Write-Verbose("Getting New-Folder string to create Script Block") - $newFolderString = Add-ScriptBlockInjection @scriptBlockInjectParams ` - -PrimaryScriptBlock ${Function:New-Folder} ` - -CatchActionFunction ${Function:Invoke-CatchBlockActions} - Write-Verbose("Creating script block") - $newFolderScriptBlock = [scriptblock]::Create($newFolderString) + Write-Verbose("New-Item create Script Block") + $newFolderScriptBlock = { param($path) New-Item -ItemType Directory -Path $path -Force | Out-Null } $serverArgListExchangeInstallDirectory = @() $serverArgListDirectoriesToCreate = @() @@ -402,9 +397,10 @@ Function Write-LargeDataObjectsOnMachine { ArgumentList = $null } + # Use , prior to the array to make sure it doesn't unwrap $serverArgListDirectoriesToCreate += [PSCustomObject]@{ ServerName = $serverName - ArgumentList = @(@("$Script:RootFilePath$serverName\Exchange_Server_Data\Config", "$Script:RootFilePath$serverName\Exchange_Server_Data\WebAppPools"), $false) + ArgumentList = (, @("$Script:RootFilePath$serverName\Exchange_Server_Data\Config", "$Script:RootFilePath$serverName\Exchange_Server_Data\WebAppPools")) } } @@ -438,7 +434,7 @@ Function Write-LargeDataObjectsOnMachine { $rootTempLocation = "{0}{1}" -f $localServerTempLocation, $serverName Write-Verbose("Local Root Temp Location: {0}" -f $rootTempLocation) #Create the temp location and write out the data - New-Folder -NewFolders $rootTempLocation + New-Item -ItemType Directory -Path $rootTempLocation -Force | Out-Null Write-ExchangeObjectDataLocal -ServerData $serverData -Location $rootTempLocation Get-ChildItem $rootTempLocation | ForEach-Object { @@ -465,7 +461,7 @@ Function Write-LargeDataObjectsOnMachine { } $location = "{0}{1}\Exchange_Server_Data" -f $Script:RootFilePath, $exchangeServerData.ServerName [array]$createFolders = @(("{0}\Config" -f $location), ("{0}\WebAppPools" -f $location)) - New-Folder -NewFolders $createFolders -IncludeDisplayCreate $true + New-Item -ItemType Directory -Path $createFolders -Force | Out-Null Write-ExchangeObjectDataLocal -Location $location -ServerData $exchangeServerData $passInfo = @{