오류메시지
이 시스템에서 스크립트를 실행할 수 없으므로 C:\Scripts\GetDirectcloudSyncState.ps1 파일을 로드할 수 없습니다.
자세한 내용은 http://go.microsoft.com/fwlink/?LinkID=135170의 about_Execution_Policies를 참조하십시오.
처리 :
파워셀로 명령어 처리시 수행.
Set-ExecutionPolicy Unrestricted
참고사이트
http://www.roelvanlisdonk.nl/?p=1129
Solution
In development set the execution policy to Unrestricted.
Set-ExecutionPolicy Unrestricted
PowerShell script (runs a function from an custom assembly)
Param
(
[String]$Connection = "", #The database connectionstring
[String]$Folder = "" #The AppPool UserName
)
[System.Reflection.Assembly]::LoadWithPartialName("Ada.Cdf")
$dd = new-object Ada.Cdf.Deployment.DatabaseDeployment
$dd.ExecuteReCreatableScriptsInFolder($Connection, $Folder)
Unittest
[Test] [Explicit("Not a unittest")] public void RunExecuteReCreatableScriptsInFolder() { List<string> parameters = new List<string>(){@"Data Source=.;Initial Catalog=TestDB;Integrated Security=SSPI;", @"C:\Temp"}; // Run a PowerShell script with two parameter and set execution policy to Unrestricted RunPowershellScript(@"C:\Temp\Test.ps1", parameters); }
Function
private static void RunPowershellScript(string scriptFile, List<string> parameters)
{
// Validate parameters
if (string.IsNullOrEmpty(scriptFile)) { throw new ArgumentNullException("scriptFile"); }
if (parameters == null) { throw new ArgumentNullException("parameters"); }
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
Command scriptCommand = new Command(scriptFile);
Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
foreach (string scriptParameter in parameters)
{
CommandParameter commandParm = new CommandParameter(null, scriptParameter);
commandParameters.Add(commandParm);
scriptCommand.Parameters.Add(commandParm);
}
pipeline.Commands.Add(scriptCommand);
Collection<PSObject> psObjects;
psObjects = pipeline.Invoke();
}
}