我正在从ConnectWise提供的API中提取数据。我正在尝试提取用户名和密码的列表,并将其设置为一种格式,可以在另一个脚本(或与此脚本的组合器)中引用该格式,也可以将其导出为CSV。如果可能的话,我希望第一个。
代码:
Function Get-CWConfiguration
{
[string]$BaseUri = "$CWServerRoot"
[string]$Accept = "application/vnd.connectwise.com+json; version=v2015_3"
[string]$ContentType = "application/json"
[string]$Authstring = $CWInfo + '+' + $CWCredentials1 + ':' + $CWCredentials2
$encodedAuth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(($Authstring)));
$Headers=@{"Authorization"="Basic $encodedAuth"}
Invoke-RestMethod -URI $BaseURI -Headers $Headers -ContentType $ContentType -Method Get
}
$username = (Get-CWConfiguration).questions | Where-Object {($_.questionid -eq 419)} | Sort-Object -Property questionid | select answer
$password = (Get-CWConfiguration).questions | Where-Object {($_.questionid -eq 420)} | Sort-Object -Property questionid | select answer
$username
变量上方的所有内容都是连接到API的内容,我没有进行任何修改。下面的代码是我发现提取所需信息的最佳方法。这会将每个变量作为数组返回,这很好,但是如果发生这种情况,我需要有一个包含2列的数组。 1个名为“用户名”,另一个名为“密码”。如果我执行$username | Get-Member
,它将返回:
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
answer NoteProperty string answer=admin@admin.com
我的最终目标是让类似$credentials
的变量输出如下内容:
Username Password
-------- ----------
admin1@admin.com Password1
admin2@admin.com Password2
admin3@admin.com Password3
我尝试过的大多数方法都将所有内容输出到1行或1列。我不确定是否应该合并数组并重命名$username.answer
和$password.answer
字段或创建PSObject
?我的主要困难在于,两个变量具有相同的answer
NoteProperty
答案 0 :(得分:0)
您要使用当前对象的扩展属性创建新对象。看起来可能都是这样(请注意,我正在为您正在处理的内容手动创建类似物,而忽略了可能需要执行的字符串操作)
$Username = New-Object -TypeName pscustomobject -Property @{Answer = "Answer:Hithisispatrick"}
$Password = New-Object -TypeName pscustomobject -Property @{Answer = "Answer:HelloThisisSquidward"}
$UsernameAnswer = $Username | Select -ExpandProperty Answer
$PasswordAnswer = $Password | Select -ExpandProperty Answer
$Final = New-Object -TypeName pscustomobject -Property @{UserName= $UsernameAnswer ; Password = $PAsswordAnswer}
这应该使您输出如下内容:
UserName Password
-------- --------
Answer:Hithisispatrick Answer:HelloThisisSquidward
然后,如果要对其进行参数化/扩展,则应该能够将其扔到foreach循环中,并使其输出“自定义对象”,将其设置为等于然后从这些对象中构建的数组,例如如此(我将使用while使其更易于显示):
$Count = 0
$Username = New-Object -TypeName pscustomobject -Property @{Answer = "Answer:Hithisispatrick"}
$Password = New-Object -TypeName pscustomobject -Property @{Answer = "Answer:HelloThisisSquidward"}
$Array = While ($Count -lt 5){
$UsernameAnswer = $Username | Select -ExpandProperty Answer
$PAsswordAnswer = $Password | Select -ExpandProperty Answer
$Final = New-Object -TypeName pscustomobject -Property @{UserName= $UsernameAnswer ; Password = $PAsswordAnswer}
$Final
$Count += 1
}
$Array
这将产生如下内容:
Password UserName
-------- --------
Answer:HelloThisisSquidward Answer:Hithisispatrick
Answer:HelloThisisSquidward Answer:Hithisispatrick
Answer:HelloThisisSquidward Answer:Hithisispatrick
Answer:HelloThisisSquidward Answer:Hithisispatrick
Answer:HelloThisisSquidward Answer:Hithisispatrick