Powershell如何传递参数

时间:2018-12-26 16:10:41

标签: powershell

我在如下模块中有一个功能:

function Update-QlikUser {
  [CmdletBinding()]
  param (
    [parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True,Position=0)]
    [string]$id,

    [string[]]$customProperties,
    [string[]]$tags,
    [string]$name,
    [string[]]$roles
  )

  PROCESS {
    $user = Get-QlikUser $id -raw
    If( $roles ) { $user.roles = $roles }
    If( $name ) { $user.name = $name }
    If( $customProperties ) {
      $user.customProperties = @(GetCustomProperties $customProperties)
    }
    If( $tags ) {
      $user.tags = @(GetTags $tags)
    }
    $json = $user | ConvertTo-Json -Compress -Depth 10
    return Invoke-QlikPut "/qrs/user/$id" $json
  }
}

现在,我尝试传递参数-customProperties,如下所示:

Update-QlikUser -id "270007b2-8c8e-4b33-8b1f-b4ba7b714c95" -customProperties "User"

现在我知道这是错误的,因为必须指定自定义属性名称,并且还必须提供该名称的值。但是我不知道语法,甚至不知道要搜索什么,这就是为什么我需要您的帮助。

编辑: 下面是getCustomProperty函数:

function Get-QlikCustomProperty {
  [CmdletBinding()]
  param (
    [parameter(Position=0)]
    [string]$id,
    [string]$filter,
    [switch]$full,
    [switch]$raw
  )

  PROCESS {
    $path = "/qrs/custompropertydefinition"
    If( $id ) { $path += "/$id" }
    If( $full ) { $path += "/full" }
    If( $raw ) { $rawOutput = $true }
    return Invoke-QlikGet $path $filter
  }
}

我在网上找到的一个例子如下。请检查最后一行。 Update-QlikUser是我要使用customproperty参数进行的操作

$importdata = $null

$files = Get-ChildItem $csvpath
$importdata = $files | ForEach  { Import-Csv $_  }


$allSeCpValues = $importdata.SE_Value | sort -unique
Update-QlikCustomProperty -id $(Get-QlikCustomProperty -filter "name eq '$sheetexceptioncp'").id -choiceValues $allSeCpValues

$allvaluescp = $importdata.Username | sort -unique

foreach ($userid in $allvaluescp) {
    $allcpperuser = $importdata | Where-Object { $_.Username -eq $userid }

    $allcpperuser2 = $allcpperuser.SE_Value | sort -unique | Where-Object { $_ -ne '' } | ForEach-Object {"$sheetexceptioncp=$_"}

    [array]$stringcpperuser = $null

    foreach( $cpvalue in $allcpperuser2) {
        $stringcpperuser += $cpvalue
    }

    $previousCPvalues = $(Get-QlikUser -filter "userid eq '$userid'" -full).customProperties | Where-Object { $_.definition.name -ne "$sheetexceptioncp" }

    foreach ($previousvalue in $previousCPvalues) {

        $prev = $previousvalue.definition.name + "=" + $previousvalue.value
        write-host the following previous custom properties was found:
        write-host $prev
        $stringcpperuser += $prev

    }


    write-host adding all this information to the users custom properties
    write-host $stringcpperuser


    Update-QlikUser -id $(Get-QlikUser -filter "userid eq '$userid'" -full).id -customProperties @($stringcpperuser)

}

1 个答案:

答案 0 :(得分:0)

我要做的就是:

Update-QlikUser -id $(Get-QlikUser -filter "userid eq '$userid'" -full).id -customProperties 'Testing=Test1'

或者简单地:

Update-QlikUser -id "270007b2-8c8e-4b33-8b1f-b4ba7b714c95" -customProperties 'Testing=Test1'