I have the following script, consisting two functions and some log writing.
All works well, with exception of the catch in the invoke-command, that will write a failure log but only to $failpath remote machine, which isnt really useful
function Get-TimeStamp {
return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}
$shareHD = {
$mydate=get-date
write-host "date is $mydate"
$goodDate=Get-Date -format "dd/MM/yyyy"
generate share name as a hidden
$share =$using:mySam+"$"
$shareperm=$using:mySam
$path=$using:PhysHD
$path="e:\swhome\swmcphersont1"
$remoteserver="rchome"
Write-Host "debug we are going to share $path as $share"
Create Share
IF (!(Get-SmbShare -Name $share -ErrorAction SilentlyContinue)) {
write-host "Creating share: " $share "`n" -ForegroundColor magenta
try
{
#New-SmbShare –Name $share –Path $path –Description $share' home directory created '$mydate –FullAccess 'Domain Users' -ErrorAction stop
New-SmbShare –Name $share –Path $path –Description $share' home directory created '$mydate –FullAccess $shareperm -ErrorAction stop
write-host "The share $share created successfully`n" -ForegroundColor green
}
catch
{
write-host "failed creating share on $remoteserver, try running creatADshare.ps1" -ForegroundColor red
# write-host "failed creating share on $remoteserver, try running creatADshare.ps1" -ForegroundColor red
#write this to the local path
write-host "fail log path is $using:failpath"
write-Output "$using:exectime Unable to create $share on $remoteserver" | Out-file $using:failpath -append
}
} else {
write-host "The share already exists: " $share "`n" -ForegroundColor Yellow
}
}
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
set up logging
$currentscript=$MyInvocation.MyCommand
$logpath="$scriptpath\logs\ADsharerRun.log"
$failpath="$scriptpath\logs\fail.log"
$mySam="swmcphersont1"
$physHD="e:\swhome\swmcphersont1"
Write-Host "$(Get-TimeStamp) run log is $logpath"
Write-Host "fail log is $failpath"
Write-Output "$(Get-TimeStamp) started $currentscript" | Out-file $logpath -append
share it if necessary
make my timestamp
$exectime=$(Get-TimeStamp)
write-host "stamp is $exectime"
Invoke-Command -ComputerName VMSG-FSRCHOME -scriptblock $shareHD
Write-Output "$(Get-TimeStamp) completed $currentscript" | Out-file $logpath -append