将C#代码(大约15行代码)转换为Powershell是否容易? [英] Is it easy to convert (about 15 lines of code) c# code to Powershell

查看:225
本文介绍了将C#代码(大约15行代码)转换为Powershell是否容易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现

I found this code online and wondering if it's easy to convert to Powershell. I am getting this error

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#代码:

For reference, here's the C# code I'm trying to convert:

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();
    }
}

推荐答案

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

This should fix the error you're getting:

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

这篇关于将C#代码(大约15行代码)转换为Powershell是否容易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆