Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, November 15, 2019 10:02 AM
I have function for getting MicrosoftSubscriptionData from Partner Center
function Export-MicrosoftSubscriptionData
{[CmdletBinding()] param ( [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string[]]$CustomerId #TODO: Add optional parameters
)
begin {
$ArrayOfSubscriptions =
[System.Collections.ArrayList]@()
}
process {
Try {
$CustomerId.ForEach({
#$CurrentCustomerId = $_
$CurrentCustomerId = "2"
$Subscriptions = Get-PartnerCustomerSubscription -
CustomerId $CurrentCustomerId
$Subscriptions.ForEach({
$Subscription = [Order]::new()
$Subscription.PopulateMicrosoftPartnerData($_.SubscriptionId, $CurrentCustomerId)
$ArrayOfSubscriptions.Add($Subscription)
})
})
}
Catch {
Write-Host "Caught an exception:" -ForegroundColor Red
Write-Host "Exception Type:
$($_.Exception.GetType().FullName)" -ForegroundColor Red
Write-Host "Exception Message: $($_.Exception.Message)" -
ForegroundColor Red errorLog $LogPath "Exporting" "subscription for Customer:$ArrayOfSubscriptions failed" }
}
end {
}
In case some of customerID fails i want to write it in some log file, so i created following function
$LogPath = Get-Location
function errorLog {
param([string]$LogPath, [string]$Msg, [string]$exportType)
if((Test-Path "$LogPath\$exportType.log") -eq $false) {New-Item "$LogPath\$exportType.log" -ItemType File}
Write-Host "$(Get-Date): $Msg $exportType" -ForegroundColor Green
Add-Content "$LogPath\$exportType.log" -Value "$(Get-Date): $Msg $exportType failed"
}
Then i called it in Catch block
I tried to simulate error, provided dummy CustomerID
Catch {
Write-Host "Caught an exception:" -ForegroundColor Red
Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
errorLog $LogPath "Exporting" "subscription failed"
}
This works without error, and it writes in log file that export failed for every Customer
11/15/2019 10:27:55: Exporting subscription failed
So i added CurrentCustomerID variable to errorLog function
Catch {
Write-Host "Caught an exception:" -ForegroundColor Red
Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
errorLog $LogPath "Exporting" "subscription for Customer:$CurrentCustomerId failed"
}
Now i'm getting
New-Item : The given path's format is not supported.
At line:11 char:58
+ ... og") -eq $false) {New-Item "$LogPath\$exportType.log" -ItemType File}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-Item], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.NewItemCommand
So error happens only when i specify CustomerID in error function, is there any way to fix this ?
All replies (2)
Friday, November 15, 2019 10:24 AM ✅Answered
Solved, stupid me: had to remove ":" and it started working
Friday, November 15, 2019 10:37 AM
Before continuing please fix you original post and make it readable.
Also note that there is never a need to create a file before using it. The first time the file is added to it will be created.
In your code there is no need to use a function. Put the code in the "Catch" and use it directly. I also recommend adding a timestamp to the log records.
\(ツ)_/