powershellimport-csvget-aduser

powershell import csv data x,y,z to command not working


powershell import csv data x,y,z to command not working

i call function Set-LogonHours via import-csv

Import-Csv .\data.csv | % {
    Set-LogonHours -Identity $_.user -TimeIn24Format @($_.time) -Monday -Tuesday -Wednesday -Thursday -NonSelectedDaysare NonWorkingDays
}

Result: error

Set-LogonHours : Cannot validate argument on parameter 'TimeIn24Format'. The 1234567 argument is greater than the maximum allowed range of 23. Supply an
argument that is less than or equal to 23 and then try the command again.

#csv data

"user","TimeIn24Format"
"testvpn","1,2,3"
Function Set-LogonHours {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $True)]
        [ValidateRange(0, 23)]
        $TimeIn24Format,
        [Parameter(Mandatory = $True,
            ValueFromPipeline = $True,
            ValueFromPipelineByPropertyName = $True, 
            Position = 0)]$Identity,
        [Parameter(Mandatory = $False)]
        [ValidateSet("WorkingDays", "NonWorkingDays")]$NonSelectedDaysare = "NonWorkingDays",
        [Parameter(Mandatory = $false)][switch]$Sunday,
        [Parameter(Mandatory = $false)][switch]$Monday,
        [Parameter(Mandatory = $false)][switch]$Tuesday,
        [Parameter(Mandatory = $false)][switch]$Wednesday,
        [Parameter(Mandatory = $false)][switch]$Thursday,
        [Parameter(Mandatory = $false)][switch]$Friday,
        [Parameter(Mandatory = $false)][switch]$Saturday
    )

    Process {
        $FullByte = New-Object "byte[]" 21
        $FullDay = [ordered]@{}
        0..23 | foreach { $FullDay.Add($_, "0") }
        $TimeIn24Format.ForEach({ $FullDay[$_] = 1 })
        $Working = -join ($FullDay.values)
        Switch ($PSBoundParameters["NonSelectedDaysare"]) {
            'NonWorkingDays' { $SundayValue = $MondayValue = $TuesdayValue = $WednesdayValue = $ThursdayValue = $FridayValue = $SaturdayValue = "000000000000000000000000" }
            'WorkingDays' { $SundayValue = $MondayValue = $TuesdayValue = $WednesdayValue = $ThursdayValue = $FridayValue = $SaturdayValue = "111111111111111111111111" }
        }
        Switch ($PSBoundParameters.Keys) {
            'Sunday' { $SundayValue = $Working }
            'Monday' { $MondayValue = $Working }
            'Tuesday' { $TuesdayValue = $Working }
            'Wednesday' { $WednesdayValue = $Working }
            'Thursday' { $ThursdayValue = $Working }
            'Friday' { $FridayValue = $Working }
            'Saturday' { $SaturdayValue = $Working }
        }
        $AllTheWeek = "{0}{1}{2}{3}{4}{5}{6}" -f $SundayValue, $MondayValue, $TuesdayValue, $WednesdayValue, $ThursdayValue, $FridayValue, $SaturdayValue
        # Timezone Check
        if ((Get-TimeZone).baseutcoffset.hours -lt 0) {
            $TimeZoneOffset = $AllTheWeek.Substring(0, 168 + ((Get-TimeZone).baseutcoffset.hours))
            $TimeZoneOffset1 = $AllTheWeek.SubString(168 + ((Get-TimeZone).baseutcoffset.hours))
            $FixedTimeZoneOffSet = "$TimeZoneOffset1$TimeZoneOffset"
        }
        if ((Get-TimeZone).baseutcoffset.hours -gt 0) {
            $TimeZoneOffset = $AllTheWeek.Substring(0, ((Get-TimeZone).baseutcoffset.hours))
            $TimeZoneOffset1 = $AllTheWeek.SubString(((Get-TimeZone).baseutcoffset.hours))
            $FixedTimeZoneOffSet = "$TimeZoneOffset1$TimeZoneOffset"
        }
        if ((Get-TimeZone).baseutcoffset.hours -eq 0) {
            $FixedTimeZoneOffSet = $AllTheWeek
        }
        $i = 0
        $BinaryResult = $FixedTimeZoneOffSet -split '(\d{8})' | Where { $_ -match '(\d{8})' }
        Foreach ($singleByte in $BinaryResult) {
            $Tempvar = $singleByte.tochararray()
            [array]::Reverse($Tempvar)
            $Tempvar = -join $Tempvar
            $Byte = [Convert]::ToByte($Tempvar, 2)
            $FullByte[$i] = $Byte
            $i++
        }
        Set-ADUser  -Identity $Identity -Replace @{logonhours = $FullByte }                                   
    }
    
    end {
        Write-Output "All Done :)"
    }
}

ref: https://www.powershellcenter.com/2021/03/09/manage-ad-user-logon-hours-using-powershell/

import csv data to command


Solution

  • Make sure the declare the target parameter an array type:

    param(
      [Parameter(Mandatory = $True)]
      [ValidateRange(0, 23)]
      [int[]]$TimeIn24Format, # <-- now the parameter can accept multiple integers between 0 and 23
      # ...
    )
    

    And then make sure you split the input string into the individual numbers:

    Import-Csv .\data.csv | % {
        Set-LogonHours -Identity $_.user -TimeIn24Format $_.time.Split(',') -Monday -Tuesday -Wednesday -Thursday -NonSelectedDaysare NonWorkingDays
    }