将c#代码转换为Powershell是不是容易(大约15行代码)

Posted

技术标签:

【中文标题】将c#代码转换为Powershell是不是容易(大约15行代码)【英文标题】:Is it easy to convert (about 15 lines of code) c# code to Powershell将c#代码转换为Powershell是否容易(大约15行代码) 【发布时间】:2012-07-03 11:26:58 【问题描述】:

我在网上找到了this code,想知道转换到 Powershell 是否容易。我收到此错误

Missing expression after ','.
At C:\AddaItem.ps1:60 char:73
+         $newFile = $docLibrary.RootFolder.Files.Add($newDestinationFolderPath, <<<<
UTF8Encoding.UTF8.GetBytes(builder.ToString()), $true)
    + CategoryInfo          : ParserError: (,:String) [], ParseException
    + FullyQualifiedErrorId : MissingExpressionAfterToken

它似乎在抱怨这条线:

$newFile = $docLibrary.RootFolder.Files.Add($newDestinationFolderPath,UTF8Encoding.UTF8.GetBytes(build
er.ToString()), $true)  

这是整个代码:

if((Get-PSSnapin | Where $_.Name -eq "Microsoft.SharePoint.PowerShell") -eq $null)

    Add-PSSnapin Microsoft.SharePoint.PowerShell

$SiteUrl = "http://portal.memorial.hermann/patient"
$web = Get-SPWeb $SiteUrl
$Library = "Unpaid Billing Records"
$docLibrary = $web.Lists[$Library]
$rootFolder = $docLibrary.RootFolder
$csvFile = "C:\\PowerShell\insurancefile.csv"
# $fileURL

foreach($i in Import-CSV $csvFile)

    $sourceFile = Get-ChildItem $i.DocLink
    $destinationFolderPath = $rootFolder.Url
    if($i.DestinationFolder -ne "")
        $destinationFolderPath += "/" + $i.DestinationFolder.replace("\","/")

        $folders = $i.DestinationFolder.Split("\")

        $subFolder = $rootFolder
        foreach($f in $folders)
        
            $testFolder = $subFolder.SubFolders[$f];
            if($testFolder -eq $null)
            
                $subFolder = $subFolder.SubFolders.Add($f)
                "created new folder " + $f
            
            else
            
                $subFolder = $testFolder
            
        
    
    $destinationFolderPath += "/" + $sourceFile.Name

    "copying " + $destinationFolderPath
    if($i.ContentType = "MasterDocument")
    
        $itemType = $docLibrary.ContentTypes[$i.ContentType]
        $newFile = $docLibrary.RootFolder.Files.Add($destinationFolderPath,$sourceFile.OpenRead(), $true)
        $theItem = $newFile.Item
        $theItem["ContentTypeId"] = $itemType.Id
        # $fileURL = $theItem.Url
    
    elseif($i.ContentType = "Link2Document")
    
        $itemType = $docLibrary.ContentTypes[$i.ContentType]
        $newDestinationFolderPath = "/" + $i.fileNameASPX
        $newFile = $docLibrary.RootFolder.Files.Add($newDestinationFolderPath,UTF8Encoding.UTF8.GetBytes(builder.ToString()), $true)    
        $theItem = $newFile.Item
        $theItem["ContentTypeId"] = $itemType.Id
        $itemUrl = New-object Microsoft.SharePoint.SPFieldUrlValue      
        $itemUrl.Url = $i.fileLinkUrl
        $itemUrl.Descrition = $i.URLDESC
        $theItem["URL"] = $itemUrl      
    

    # UPDATING METADATA 
    # $theItem["Name"] = $i.newfilename #Rename file name
    $theItem["Status"] = $i.Status
    $theItem["Title"] = $i.Title
    $theItem["Grantor"] = $i.Grantor

    $theItem.Update()


$web.Dispose()

作为参考,这是我要转换的 C# 代码:

using ( SPSite siteCollection = new SPSite( "http://moss.litwareinc.com" ) ) 
    using ( SPWeb web = siteCollection.OpenWeb( "docs" ) ) 
        SPList list = web.Lists["Sample"];

        //link to the file
        string fileLinkUrl = "http://moss.litwareinc.com/docs/Shared%20Documents/ConfigureIRMinWSS30.doc";

        StringBuilder builder = new StringBuilder();

        using ( TextReader reader = new StreamReader( @"C:\linktodocumenttemplate.txt" ) ) 
            builder.Append( reader.ReadToEnd() );
        

        //replace string template with values
        builder.Replace( "0", fileLinkUrl );

        //should change the name of the .aspx file per item
        SPFile file = list.RootFolder.Files.Add( "link_title.aspx", UTF8Encoding.UTF8.GetBytes(builder.ToString()));

        //set list item properties
        SPListItem item = file.Item;
        item["Content Type"] = "Link to a Document";
        SPFieldUrlValue itemUrl = new SPFieldUrlValue();
        itemUrl.Description = "From sample code";
        itemUrl.Url = fileLinkUrl;
        item["URL"] = itemUrl;
        //persist changes
        item.Update();
    

【问题讨论】:

请花时间解释什么“不起作用”以及您需要帮助的具体内容。 对不起,我完全忘了添加错误。我更新了帖子。 这样更好,但也请包含您的 powershell 代码。 @KirkWoll:是的,先生。我在这里上传了我的powershell代码wikisend.com/download/429800/Add_Files_PS.txt 谢谢,但以后,请像我为您所做的那样将代码包含在内。 【参考方案1】:

这应该可以解决您遇到的错误:

$content = get-content "C:\linktodocumenttemplate.txt"
$utf = new-object System.Text.UTF8Encoding
$newFile = $docLibrary.RootFolder.Files.Add($newDestinationFolderPath, $utf.GetBytes($content.ToString()), $true)

【讨论】:

非常感谢您的帮助。

以上是关于将c#代码转换为Powershell是不是容易(大约15行代码)的主要内容,如果未能解决你的问题,请参考以下文章

Powershell pscustomobject 格式-表格新行而不是一行

powershell psobject显示为字符串而不是psobject,如何转换回psobject

将 Rgb 图像转换为灰度 C# 代码时的性能问题

使用 Powershell .Net 方法将 XML 转换为 HTML

将Powershell中的输入/输出编码强制转换为特定的区域设置/代码页?

C# 如何将 CSV 转换为数组?