我已经编写了VC ++代码来在远程服务器上运行Powershell脚本。我首先创建一个PSSession到服务器,然后我运行大约30000个命令行开关。执行此操作时,我收到System.outofmemory异常。我也试图增加MaxMemoryPerShellMB'到2048年,但这并没有解决问题。好心提醒。这是我的代码:
runSpace = RunspaceFactory::CreateRunspace();
runSpace->Open();
ps = PowerShell::Create();
ps->Runspace = runSpace;
PSCommand^ rmcommand = gcnew PSCommand();
Uri^ uri = gcnew Uri("http://"+ serverName +"/powershell?serializationLevel=Full");
rmcommand->AddCommand("New-PSSession");
rmcommand->AddParameter("ConfigurationName", "Microsoft.Exchange");
rmcommand->AddParameter("ConnectionUri", uri);
rmcommand->AddParameter("Authentication", "kerberos");
rmcommand->AddParameter("Name", "pssession");
PSSessionOption^ sessionOption = gcnew PSSessionOption();
sessionOption->SkipCACheck = true;
sessionOption->SkipCNCheck = true;
sessionOption->SkipRevocationCheck = true;
rmcommand->AddParameter("SessionOption", sessionOption);
ps->Commands = rmcommand;
Collection<PSObject^>^ result = ps->Invoke();
rmcommand = gcnew PSCommand();
rmcommand->AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned");
ps->Commands = rmcommand;
ps->Invoke();
rmcommand = gcnew PSCommand();
rmcommand->AddScript("Import-PSSession -Session $session -CommandName 'Get-MailboxStatistics'");
ps->Commands = rmcommand;
ps->Invoke();
PSCommand^ command = gcnew PSCommand();
for(size i = 0; i < 30000; i++)
{
mbxidentity=mailbox[i];
command = gcnew PSCommand();
command->AddScript("Get-MailboxStatistics -Identity \'" + mbxidentity + "\' | select LegacyDN");
ps->Commands = command;
Collection<PSObject^>^ result = ps->Invoke();
//Do something with result
}
这里&#39;邮箱&#39;是包含邮箱标识列表的数组。另外,我在不同的地方获得Outofmemory。有时我在运行New-PSSession时会得到它。有时,大约1000个命令行程序成功运行,然后我得到异常。可能是什么问题?我检查了远程服务的内存,没关系。
答案 0 :(得分:0)
我对使用c ++ / c#运行powershell代码不是很有经验。我的评论越来越大,所以我会用答案来提问等。
Get-Mailbox
?我无法看到你将输出存储在任何地方,这可能会产生很多对象=内存使用