I am trying to create a script so remote users can run search-csclslogging from their computers instead of going to the server and running the command themselves. Unfortunately, I am getting the following error:
Cannot bind parameter 'StartTime'. Cannot convert value """" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
Script below
# Enter your domain.org login
$cred = Get-Credential
# Get current date and time in desired format
$currentTime = Get-Date -Format "MMddyyyy_HHmmss"
# Define the base log filename
$baseLogName = Read-Host "Please enter the contact id"
# Define the search parameters
$startdateString = Read-Host "Enter the start date/time in format MM/dd/yyy"
$startDate = Get-Date -Format "M/d/yyyy" -date $startdateString
$startTime = Read-Host "Enter the start time in the format hh:mmAM/PM"
$enddateString = Read-Host "Enter the end date in format MM/dd/yyyy"
$endDate = Get-Date -Format "M/d/yyyy" -date $enddateString
$endtime = Read-Host "Enter the end time in the format hh:mmAM/PM"
# Combining search parameters
$startCombined = $startDate + " " + $startTime
$endCombined = $endDate + " " + $endTime
# Combining datetime and log name
$combinedLogName = $baseLogName + "_" + $currentTime
# Combine base name and timestamp for remote log path
$remoteLogPath = "\\server.domain.org\c$\Logs\$combinedLogName.clslog"
# Define the local log path
$localLogPath = "C:\Logs\$combinedLogName.clslog"
# Run Search-CsClsLogging on the remote server and redirect output to the log file
Invoke-Command -ComputerName server.domain.org -Credential $cred -ScriptBlock {
Search-CsClsLogging -Pools `"pool1.domain.org`", `"pool2.domain.org`", `"pool3.domain.org`" -StartTime `"$startCombined`" -EndTime `"$endCombined`" -OutputFilePath `"$localLogPath`"
}
# Copy the log file from the remote server to the local machine
Copy-Item -Path `"$remoteLogPath`" -Credential $cred -Destination `"$localLogPath`" -Force
# Delete the remote log file
Invoke-Command -ComputerName server.domain.org -Credential $cred -ScriptBlock {
Remove-Item -Path `"$localLogPath`" -Force
}
# Write a confirmation message
Write-Host "Search-CsClsLogging results copied to: $localLogPath"
Write-Host "Remote log file deleted: $remoteLogPath"
I'm sure that's not the only issue that I will encounter with this, but any help would be appreciated!
I'm expecting the following user input:
startdateString = 03/01/2024 startTime = 12:15PM enddateString = 03/01/2024 endTime = 12:17PM
If I run the defining and combining search parameters, I get the following outputs:
3/1/2024 12:15PM 3/1/2024 12:17PM
And then feeding these into the Search-CsClsLogging I should get the following (but I get the error instead):
Search-CsClsLogging -Pools "pool1.domain.org", "pool2.domain.org", "pool3.domain.org" -StartTime "3/1/2024 12:15PM" -EndTime "3/1/2024 12:17PM" -OutputFilePath "C:\Logs\1234_03012024_140656.clslog"