该字符串缺少终结符:“

时间:2013-04-23 17:07:43

标签: c# exception powershell

当我调用此脚本时,给我一个例外

  

字符串缺少终结符:“

 using (var powershell = PowerShell.Create())
                {
                    powershell.Runspace = myRunSpace;
                    Collection<ErrorRecord> errors = null;
                    Collection<PSObject> result = null;

                    string psScript = @"
                                            $ApplicationID=" + applicationID +
                                      "\n" +
                                      "$ApplicationPort" + port + "\n" +
                                      "$Registrar=" + registrar + "\n" +
                                      "$NewPoolIdentity=" + poolIdentity + "\n" +
                                      "$ScriptName =" + scriptName + "\n" +
                                      "$SiteName= (get-cspool -Identity $Registrar).Site" + "\n" +
                                      "$Site= (get-cssite -Identity $SiteName).SiteId" + "\n" +
                                      "$ComputerFqdn =[System.Net.Dns]::GetHostByName(($env:computerName)).HostName" +
                                      "\n" +
                                      "$com=New-Object -ComObject CertificateAuthority.Config" + "\n" +
                                      "$CertificationAuthority = $com.GetConfig(0)" + "\n" +
                                      "$CertificateFriendlyName =$NewPoolIdentity +\" Pool" + "\n" +
                                      "$ApplicationFullID =$ApplicationID+ $ApplicationPort" + "\n" +
                                      "$DefaultSipDomain = (Get-CsSipDomain | where-object { $_.IsDefault -eq $true}).Name" +
                                      "\n" +
                                      "$ApplicationSipAddress = \"sip:\" + $ApplicationFullID + \"@\" + $DefaultSipDomain" +
                                      "\n" +
                                      "New-CsTrustedApplicationPool -Identity $NewPoolIdentity -Registrar $Registrar -RequiresReplication $false -Site $Site -ComputerFqdn $ComputerFqdn" +
                                      "\n" +
                                      "$MyTrustedApplications=New-CSTrustedApplication –ApplicationId $ApplicationFullID -TrustedApplicationPoolFqdn $NewPoolIdentity -Port $ApplicationPort" +
                                      "\n" +
                                      "Enable-CsTopology" + "\n" +
                                      "$Cert=Request-CsCertificate -New -Type default -FriendlyName $CertificateFriendlyName -CA $CertificationAuthority -ComputerFQDN $ComputerFqdn" +
                                      "\n" +
                                      "Set-CsCertificate -Type Default -Thumbprint $Cert.Thumbprint" + "\n" +
                                      "$ApplicationIdentity =  \"Registrar:\"+ $Registrar + \"/\" + $ApplicationFullID " +
                                      "\n" +
                                      "$ApplicationURI= \"http://www.sdsdsds.com/Apps/\" + $ApplicationID" + "\n" +
                                      "New-CsServerApplication -Identity $ApplicationIdentity -ScriptName $ScriptName -Uri $ApplicationURI -Priority 6 -Critical $False -Enabled $True" +
                                      "\n" +
                                      "New-CsTrustedApplicationEndpoint -ApplicationId $ApplicationFullID -TrustedApplicationPoolFqdn $NewPoolIdentity -SipAddress $ApplicationSipAddress" +
                                      "\n" +
                                      " $FinalResult = new-object PSObject;" +
                                      "\n" +
                                      "$FinalResult | add-member NoteProperty -Name Gruu $MyTrustedApplications.ComputerGruuS[0].Gruu;" +
                                      "\n" +
                                      "$FinalResult | add-member NoteProperty -Name LocalMachineFqdn $MyTrustedApplications.ComputerGruuS[0].Fqdn; " +
                                      "\n" +
                                      "$FinalResult | add-member NoteProperty -Name Port $MyTrustedApplications.Port; " +
                                      "\n" +
                                      "$FinalResult | add-member NoteProperty -Name ApplicationPoolFqdn $MyTrustedApplications.TrustedApplicationPoolFqdn; " +
                                      "\n" +
                                      "$FinalResult | add-member NoteProperty -Name Registrar $TrustedApplicationPool.Registrar; " +
                                      "\n" +
                                      "$FinalResult | add-member NoteProperty -Name CertificateFriendlyName $CertificateFriendlyName" +
                                      "\n" +
                                      "$FinalResult | add-member NoteProperty -Name ApplicationSipAddress $ApplicationSipAddress" +
                                      "\n" + "$FinalResult";



                    powershell.Commands.AddScript(psScript);


                    try
                    {
                        result = powershell.Invoke();
                        errors = powershell.Streams.Error.ReadAll();
                        Console.Write(".");
                    }

任何人都可以帮我找到错误

1 个答案:

答案 0 :(得分:2)

您在

附近缺少'='标志
"$ApplicationPort" + port + "\n"

处有未确定的引用
$CertificateFriendlyName =$NewPoolIdentity +" Pool

您需要对其进行编辑,以包含引号:

"$CertificateFriendlyName =$NewPoolIdentity +\" Pool" + "\"\n" +