Powershell freaky code

Powershell freaky code

\working\conf\application\scripts\powershell\ PSUtils.ps1 calculates time using substring 
  1. Function Get-SessionName([string]$SessionInit,[string]$Hostname=$null)
  2. {
    $currtime=Get-Date -UFormat "%s"
    $currtime= $currtime.substring(0,$currtime.indexof("."))
        $SessionName=$SessionInit+"_"+$currtime+"_"+$Hostname
        return $SessionName
    }
in a lot of cases (different regional settings) this will lead to exception in ANY powershell query to monitored servers
  1. Exception calling "Substring" with "2" argument(s): "Length cannot be less than  zero.
  2. Parameter name: length"
  3. At \working\conf\application\scripts\powershell\PSUtils.ps1:8 char:5
  4. +     $currtime=$currtime.substring(0,$currtime.indexof("."))
  5. +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6.     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  7.     + FullyQualifiedErrorId : ArgumentOutOfRangeException
please change  PSUtils.ps1  Get-SessionName to
  1. Function Get-SessionName([string]$SessionInit,[string]$Hostname=$null)
  2. {
  3. #$currtime=Get-Date -UFormat "%s"
  4. #$currtime=$currtime.substring(0,$currtime.indexof("."))
  5. $currtime=[int][double]::Parse((Get-Date -Uformat %s))
  6.     $SessionName=$SessionInit+"_"+$currtime+"_"+$Hostname
  7.     return $SessionName
  8. }

Thank you

                New to ADManager Plus?

                  New to ADSelfService Plus?