我正在尝试运行一个powershell命令将一些数据返回到SQL表中,但我没有运气让它工作。
具体来说,我想从SharePoint中提取User Profile Service中所有用户详细信息的列表,然后通过链接服务器连接查询LDAP并获取所有用户详细信息,以便我可以比较详细信息,因为我认为同步过程被打破了。
我试图避免将powershell脚本保存到文件中。
我有一个PowerShell脚本来获取详细信息。
$site = Get-SPSite "http://site-dev.com.au";
$context = Get-SPServiceContext $site;
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context);
$Profiles = $ProfileManager.GetEnumerator();
$Userlist = New-Object System.Collections.ArrayList;
$Userlist.Clear();
foreach($up in $Profiles)
{
$prof = New-Object PSObject;
$prof | Add-Member -type NoteProperty -Name 'SID' -Value $up.get_Item("SID");
$prof | Add-Member -type NoteProperty -Name 'ADGuid' -Value $up.get_Item("ADGuid");
$prof | Add-Member -type NoteProperty -Name 'AccountName' -Value $up.get_Item("AccountName");
$prof | Add-Member -type NoteProperty -Name 'WorkEmail' -Value $up.get_Item("WorkEmail");
[void]$Userlist.Add($prof);
}
$Userlist
这样可行,然后我将其与更多代码相结合以远程运行它。
$pwd = '***************************************************************' | ConvertTo-SecureString; $crd = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "Staff\Admin-DEV", $pwd; Invoke-Command -Computername server01.staff.ad.com.au -Authentication CredSSP -Credential $crd -ScriptBlock {Add-PSSnapin Microsoft.SharePoint.PowerShell; $site = Get-SPSite "http://site-dev.com.au"; $context = Get-SPServiceContext $site; $profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context); $Profiles = $ProfileManager.GetEnumerator(); $Userlist = New-Object System.Collections.ArrayList; $Userlist.Clear(); foreach($up in $Profiles) { $prof = New-Object PSObject; $prof | Add-Member -type NoteProperty -Name 'SID' -Value $up.get_Item("SID"); $prof | Add-Member -type NoteProperty -Name 'ADGuid' -Value $up.get_Item("ADGuid"); $prof | Add-Member -type NoteProperty -Name 'AccountName' -Value $up.get_Item("AccountName"); $prof | Add-Member -type NoteProperty -Name 'WorkEmail' -Value $up.get_Item("WorkEmail"); [void]$Userlist.Add($prof); } $Userlist }
其中$pass
是已经入选的字符串
$pass = Read-Host -AsSecureString |ConvertFrom-SecureString
但是当我接受这个并尝试从SQL运行时它就失败了。
DECLARE @command nvarchar(max)
SET @command = N'EXEC xp_cmdshell N''powershell -Command {'
SET @command = @command + N'$pwd = ''''**************************************************'''' | ConvertTo-SecureString; $crd = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "Staff\Admin-DEV", $pwd; Invoke-Command -Computername server01.staff.ad.com.au -Authentication CredSSP -Credential $crd -ScriptBlock {Add-PSSnapin Microsoft.SharePoint.PowerShell; $site = Get-SPSite "http://site-dev.cqu.edu.au"; $context = Get-SPServiceContext $site; $profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context); $Profiles = $ProfileManager.GetEnumerator(); $Userlist = New-Object System.Collections.ArrayList; $Userlist.Clear(); foreach($up in $Profiles) { $prof = New-Object PSObject; $prof | Add-Member -type NoteProperty -Name ''''SID'''' -Value $up.get_Item("SID"); $prof | Add-Member -type NoteProperty -Name ''''ADGuid'''' -Value $up.get_Item("ADGuid"); $prof | Add-Member -type NoteProperty -Name ''''AccountName'''' -Value $up.get_Item("AccountName"); $prof | Add-Member -type NoteProperty -Name ''''WorkEmail'''' -Value $up.get_Item("WorkEmail"); [void]$Userlist.Add($prof); } $Userlist }'
SET @command = @command + '}'''
EXEC sp_executeSQL @command
我收到错误
"'的ConvertTo-SecureString的'不被视为内部或 外部命令,"
我不确定这个错误是否与它失败的原因有什么关系,或者是因为我在SQL CMD和PowerShell之间传递时没有正确地转义某些字符。