Am I coding this powershell correctly? Because it does not do what I intended
Need to find out the document set name from a field name called "Region". I need to set the doc set name to be whatever Region is (NW, SW, NE, SE, etc.) and move files from root of the document library to it's respected doc set. I dont mind hard coding the site or web url and doc lib url. I am geting this error:
Missing expression after ','. At C:\PS\MoveFiles.ps1:13 char:59 + $list.Items.MoveTo($destinationFolderUrl + $file.Name, <<<< true); + CategoryInfo : ParserError: (,:String) [], ParseException + FullyQualifiedErrorId : MissingExpressionAfterToken
#Setup default variables
$webUrl = Get-SPWeb -Identity "http://CiscoIntranet/sites/VOIP"
$list = $webUrl.GetList("http://CiscoIntranet/sites/VOIP/ForwardTech")
[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
function ProcessMove {
param($folderUrl)
$folder = $web.GetFolder($folderUrl)
foreach ($file in $folder.Files)
{
$docset=$($file.Region);
$destinationFolde开发者_StackOverflowrUrl = "http://CiscoIntranet/sites/VOIP/ForwardTech/" + $docset;
$list.Items.MoveTo($destinationFolderUrl + $file.Name, true);
$webUrl.Update();
}
}
The second parameter should be $true
and not just true.
I replicated the same error as follows:
function fun($m,[bool]$f) {
write-host $m $f
}
fun ("blah", true)
Weirdly, something like:
function fun([bool]$f) {
write-host $m $f
}
fun (true)
gives an error like:
fun : Cannot process argument transformation on parameter 'f'. Cannot convert value "System.Management.Automation.PSCustomObject" to type "System.Boolean", parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.
which is more descriptive and the solution is in the error message!
This is the working code
$siteURL="http://Server"
$docLib = "My Doc Lib"
$site=Get-SPSite $siteURL
$web=$site.RootWeb
$collFiles=$web.GetFolder($docLib).Files
$count=$collFiles.Count
while($count -ne 0)
{
$item = $collFiles[$count-1].Item
$DocSet = $item["Region"]
Write-Host "$DocSet is the doc set. $collFiles[$count-1].Name is name"
$collFiles[$count-1].MoveTo($siteURL + "/" + $docLib + "/" + $DocSet + "/" + $collFiles[$count-1].Name, $true)
$count--
}
精彩评论