字符串的格式不正确C#

时间:2014-08-18 15:57:08

标签: c#

我正在使用以下代码。

MySiteScript = String.Format("$mysiteHostUrl = \"{0}\"; 
$personalSiteGlobalAdminDisplayName = \"SharePoint Admin\"; 
$mysite = Get-SPSite $mysiteHostUrl; 
$context = [Microsoft.Office.Server.ServerContext]::GetContext($mysite); 
$upm =  New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context);  
$AllProfiles = $upm.GetEnumerator(); 
foreach($profile in $AllProfiles) 
{
    $DisplayName = $profile.DisplayName; 
    $AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value;   
    if($AccountName -like \"{1}\") 
    { 
        if($profile.PersonalSite -eq $Null) 
        { 
            $profile.CreatePersonalSite();  
        } 
        else  
        { 
            Write-Host \"Personal site already exists.\" ;  
        } 
    } 
    else 
    { 
        Write-Host \"User not found.\";  
    }  
} 
$mysite.Dispose()", mySiteHostUrl, sAMAccountName);

收到错误: 字符串的格式不正确。

问题是什么?你有什么建议?

1 个答案:

答案 0 :(得分:3)

您应该使用多行字符串文字,即verbatim string

MySiteScript = String.Format(@"$mysiteHostUrl ...
...
...$mysite.Dispose()", mySiteHostUrl, sAMAccountName);

注意 - 您不能在逐字字符串中使用反斜杠转义为双引号。使用quote-escape-sequence(两个连续的双引号字符)。完整样本:

dMySiteScript = String.Format(@"$mysiteHostUrl = ""{0}""; 
$personalSiteGlobalAdminDisplayName = ""SharePoint Admin""; 
$mysite = Get-SPSite $mysiteHostUrl; 
$context = [Microsoft.Office.Server.ServerContext]::GetContext($mysite); 
$upm =  New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context);  
$AllProfiles = $upm.GetEnumerator(); foreach($profile in $AllProfiles) {{$DisplayName = $profile.DisplayName; 
$AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value;   
if($AccountName -like ""{1}"") 
  {{ 
    if($profile.PersonalSite -eq $Null) {{ $profile.CreatePersonalSite();  
    }} else  {{ 
        Write-Host ""Personal site already exists."" ;  }} 
  }} else {{ 
     Write-Host ""User not found."";  }}  
}}  $mysite.Dispose()", mySiteHostUrl, sAMAccountName);