我有一个Powershell脚本,该脚本在ISE中工作正常,但是在RunDeck中从文件运行它时,出现以下错误
字符串缺少终止符:'。
09:19:34 + CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException
09:19:34 + FullyQualifiedErrorId:TerminatorExpectedAtEndOfString
09:19:34
09:19:34'mCd'不被识别为内部或外部命令,
09:19:34可操作程序或批处理文件。
09:19:34结果:1
我不明白怎么了?这仅仅是RunDeck的限制吗? Powershell脚本确实包含XML。我什至尝试将XML内容放入一个适当的XML文件中,然后使用get-content进行一些操作。这是代码(我知道拼写错误,那就是“很好”)
Param(
[Parameter(Mandatory=$true,Position=0)]
[string]$pass,
[Parameter(Mandatory=$true)]
[string]$teamSiteTitel
)
#Credentials for Service Account
Set-Location "\\Server\c$\admin_apps\scripts\SP_IS"
#Credentials for Service Account
$credentialXml = Get-Content -Path .\cred.xml
$credential = @"
$credentialXml
"@
$tenantName =Select-Xml -Content $credential -XPath "//tenant" | foreach {$_.node.InnerXML}
$username =Select-Xml -Content $credential -XPath "//username" | foreach {$_.node.InnerXML}
$password= ConvertTo-SecureString $pass -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username, $password
$rootSite =Select-Xml -Content $credential -XPath "//rootSite" | foreach {$_.node.InnerXML}
# Connect to to Office 365
try{
Connect-PnPOnline -Url ("https://{0}-admin.sharepoint.com/" -f $tenantName) -Credential $cred
write-host "Info: Connected succesfully to Office 365" -foregroundcolor green
}
catch{
write-host "Error: Could not connect to Office 365" -foregroundcolor red
Break new-SPOnlineSite
}
#Connect to the Site Collection
try{
write-host "Connect to site collection"
Connect-PnPOnline –Url ("https://{0}.sharepoint.com/" -f $tenantName) –Credentials $cred
}catch{
write-host "Error: Could not connect to site 365" -foregroundcolor red
$tenantName
}
#Load SharePoint CSOM Assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null
#TeamSite parameter
#$teamSiteTitel = Read-Host -Prompt "Please enter the Teamsite Titel"
$template = "STS#0"
$pagesLibrary = "Pages"
$PageName="HomePage"
$PageTemplate = "CompanyName2ColumnsPageLayout"
#Create the new team site
$ParentSiteUrl = $rootSite+ "/InternalServices/"
#$ParentSiteUrl = Read-Host -Prompt "Please enter the Teamsite Parent Url"
#Connect to the Site Collection
write-host "Connect to Parent site"
Connect-PnPOnline –Url $ParentSiteUrl –Credentials $cred
Write-Output "Creating new Team Site"
$NewWeb = New-PnPWeb -Title $teamSiteTitel -Url $teamSiteTitel -Template $template -BreakInheritance
$SiteUrl= $NewWeb.Url
##Enable The Push Notifications feature
#Write-Output "Activating Notifications feature"
#Enable-PnPFeature -Identity 41e1d4bf-b1a2-47f7-ab80-d5d6cbba3092 -Scope Web
#Connect to the new team site
Write-Output "Connecting to new Team Site"
Connect-PnPOnline -Url $SiteUrl -Credentials $cred
#create Groups
Write-Output "creating groups"
$ownerGroupName = $teamSiteTitel + "_Owners"
$CompanyNameAllName = "CompanyName_Allnew"
New-PnPGroup -Title $ownerGroupName -ErrorAction SilentlyCont
New-PnPGroup -Title $CompanyNameAllName -ErrorAction SilentlyCont
#Set group permission, owner = full control, member = control
Write-Output "Setting group permissions"
Set-PnPGroupPermissions -Identity $ownerGroupName -AddRole 'Edit' -Web (Get-PnPWeb)
Set-PnPGroupPermissions -Identity $CompanyNameAllName -AddRole 'Contribute' -Web (Get-PnPWeb)
#Add team site owner to owner group
Write-Output "Adding Owner to group"
Add-PnPUserToGroup -LoginName $username -Identity $ownerGroupName
#Activate the "publishing" feature
Write-Output "Activating publishing feature"
Enable-PnPFeature -Identity 94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb -Scope Web
##Remove Getting Started and Site feed Webpart
Write-Output "Removing Getting Started Webpart"
Remove-PnPWebPart -Title "Get started with your site" -ServerRelativePageUrl "/SitePages/Home.aspx"
Remove-PnPWebPart -Title "Site Feed" -ServerRelativePageUrl "/SitePages/Home.aspx"
#Deactivate "Sit Feed" feature
Write-Output "Deactivate Site Feed feature"
Disable-PnPFeature -Identity 15a572c6-e545-4d32-897a-bab6f5846e18 -Scope Web
#Add Content type
Write-Output "Adding ContenType"
Add-PnPContentTypeToList -List $pagesLibrary -ContentType "CompanyNamePage" -DefaultContentType
Add-PnPContentTypeToList -List "Financials" -ContentType "CompanyNameDocument" -DefaultContentType
#Add Publishing page
Write-Output "Adding Publishing page"
Add-PnPPublishingPage -PageName $PageName -Title $PageName -PageTemplateName $PageTemplate -Publish
#Set the new Page as home page
Set-PnPHomePage -RootFolderRelativeUrl ("{0}/{1}.aspx" -f $pagesLibrary,$PageName)
$UrlHomePage = $NewWeb.ServerRelativeUrl + "/" + $pagesLibrary + "/" +$PageName +".aspx"
#CheckOut The Page
Set-PnPFileCheckedOut -Url $UrlHomePage
#Adding Documents Webpart
Write-Output "Adding Documents Webpart"
$scriptP= "
<webParts>
<webPart xmlns='http://schemas.microsoft.com/WebPart/v3'>
<metaData>
<type name='Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name='ExportMode' type='exportmode'>All</property>
<property name='HelpUrl' type='string' />
<property name='Hidden' type='bool'>False</property>
<property name='Description' type='string'>Allows authors to insert HTML snippets or scripts.</property>
<property name='Content' type='string'>
<script type='text/javascript'(>);
Function SP.SOD.executeFunc {('sp.js'), ('SP.ClientContext')}
Function SP.SOD.executeFunc {('sp.search.js', 'Microsoft.SharePoint.Client.Search.Query')}
alightContentSearch('latesDocuments', 'path:\''+_spPageContextInfo.webAbsoluteUrl+''\''NOT FileType:html NOT Filetype:aspx', 'Title', 'LatestDocuments', '5', ([String]{property: 'LastModifiedTime', DESC: 1}));
</script(>);
<div id='latesDocuments'(>);</div(>);</property>
<property name='CatalogIconImageUrl' type='string' />
<property name='Title' type='string'>Latest Documents</property>
<property name='AllowHide' type='bool'>True</property>
<property name='AllowMinimize' type='bool'>True</property>
<property name='AllowZoneChange' type='bool'>True</property>
<property name='TitleUrl' type='string' />
<property name='ChromeType' type='chrometype'>TitleOnly</property>
<property name='AllowConnect' type='bool'>True</property>
<property name='Width' type='unit' />
<property name='Height' type='unit' />
<property name='HelpMode' type='helpmode'>Navigate</property>
<property name='AllowEdit' type='bool'>True</property>
<property name='TitleIconImageUrl' type='string' />
<property name='Direction' type='direction'>NotSet</property>
<property name='AllowClose' type='bool'>True</property>
<property name='ChromeState' type='chromestate'>Normal</property>
</properties>
</data>
</webPart>
</webParts>"
Add-PnPWebPartToWebPartPage -ServerRelativePageUrl $UrlHomePage -XML $scriptP -ZoneId "LeftCol" -ZoneIndex 1
#CheckIn The Page
Set-PnPFileCheckedIn -Url $UrlHomePage
#Site URL
Write-Output "Team Site Url is :" $SiteUrl
答案 0 :(得分:0)
单击“高级”按钮调用PowerShell脚本(内联或外部),然后在“调用字符串”文本框中设置“ powershell.exe”,在“文件扩展名”文本框中设置“ ps1”,单击“保存”,然后在“创建”以保存您的工作。
以下是使用本地脚本和远程脚本工作的Job-Def:
<joblist>
<job>
<defaultTab>nodes</defaultTab>
<description></description>
<dispatch>
<excludePrecedence>true</excludePrecedence>
<keepgoing>false</keepgoing>
<rankOrder>ascending</rankOrder>
<successOnEmptyNodeFilter>false</successOnEmptyNodeFilter>
<threadcount>1</threadcount>
</dispatch>
<executionEnabled>true</executionEnabled>
<id>6d0867aa-fa70-44fb-b6de-d0eed06fd0dd</id>
<loglevel>INFO</loglevel>
<name>ScriptWINDOWS</name>
<nodeFilterEditable>false</nodeFilterEditable>
<nodefilters>
<filter>name: windows</filter>
</nodefilters>
<nodesSelectedByDefault>true</nodesSelectedByDefault>
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<fileExtension>ps1</fileExtension>
<scriptargs />
<scriptfile>/home/user/Rundeck/ver/3.1.2/hello.ps1</scriptfile>
<scriptinterpreter>powershell.exe</scriptinterpreter>
</command>
<command>
<exec>powershell.exe c:\users\myuser\myscript.ps1</exec>
</command>
</sequence>
<uuid>6d0867aa-fa70-44fb-b6de-d0eed06fd0dd</uuid>
</job>
</joblist>
答案 1 :(得分:0)
注意带有内联脚本的@符号,它们是Rundeck变量的占位符。
我的经验法则是内联Powershell脚本,其运行梯级包含“ @`s”。 搜索并将所有@替换为@@。它将使您的生活更轻松。
确保您的代码编辑器不同意,我倾向于保留两个脚本副本。我可以编辑的最终副本,并将@@消毒版本加载到rundeck。
在移动设备上有详细的文档,如果没有人击败我,我将在稍后更新。
编辑: 尝试将此位更新为:
f = open(filename, 'r')
content = f.read()