######################################## # Webdav Upload with Powershell ########################################
# Absolute path to file $file = "D:\text123.txt" # URL no trailing "/" $url = "https://webdav.synapse.com/Disney/beakerdata"
# Username and Password
$user = ""
$pass = ""
######################################## # Script #######################################
# Append the name of the file onto the url $url += "/" + $file.split('\')[(($file.split("\")).count - 1)]
######################################## # Begin prepare file data #######################################
Write-Host "File upload started"
# Set as binary data Set-Variable -name adFileTypeBinary -value 1 -option Constant
$objADOStream = New-Object -ComObject ADODB.Stream $objADOStream.Open() $objADOStream.Type = $adFileTypeBinary $objADOStream.LoadFromFile("$file") $arrbuffer = $objADOStream.Read()
######################################## # End prepare file data #######################################
######################################## # Begin WebDav connection and PUT #######################################
$objXMLHTTP = New-Object -ComObject MSXML2.ServerXMLHTTP.6.0 $objXMLHTTP.setOption(2,13056) # Ignore ceriticate errors $objXMLHTTP.Open("PUT", $url, $False, $user, $pass) $objXMLHTTP.send($arrbuffer)
# This will give you the response text # $objXMLHTTP.responseText
# Verify upload if ($objXMLHTTP.status -eq 201) { Write-Host "File upload finished" }
elseif ($objXMLHTTP.status -eq 204) { Write-Host "File already exists" }
# status code detail # http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms767625(v=vs.85).aspx
######################################## # End WebDav connection and PUT #######################################
这个是我的.ps1
/// <summary> /// C# Language Call the PowerShell Script /// Author:chenkai Date:2010年11月9日10:12:24 /// </summary> public class Program { static void Main(string[] args) {
string getresult= Program.OperatorObjectShell(); Console.WriteLine(getresult); Console.Read(); }
/// <summary> /// 测试C#于Shell对象操作的可操作性 /// </summary> /// <returns>返回计算结果</returns> public static string OperatorObjectShell() { string getres = string.Empty; try { //zaiShell 执行方法参数 List<string> getlist = new List<string>(); getlist.Add("Set-ExecutionPolicy RemoteSigned");//先执行启动安全策略,,使系统可以执行powershell脚本文件
string pspath = System.IO.Path.Combine(Environment.CurrentDirectory, "webdav_push.ps1"); getlist.Add(pspath);
//定义一个操作的实体对象 ConvertPatameter newconvert = new ConvertPatameter { Path = "D:\text123.txt", RetMsg="dfdf" };
if (File.Exists(pspath)) { RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace newspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
newspace.Open(); Pipeline newline = newspace.CreatePipeline(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(newspace);
Command getcmd = new Command(pspath); CommandParameter newcmdpara = new CommandParameter("Result", newconvert); getcmd.Parameters.Add(newcmdpara); newline.Commands.Add(getcmd);
//注入脚本一个.NEt对象 这样在powershell脚本中就可以直接通过$key访问和操作这个对象 //newspace.SessionStateProxy.SetVariable(getshellpar.ShellKey,getshellpar.ShellValue); newspace.SessionStateProxy.SetVariable("Result", newconvert);
//执行 var gettakeres=newline.Invoke();
foreach (var getstr in gettakeres) { Console.WriteLine(getstr.ToString()); }
Console.Write(newconvert.RetMsg.ToString()+newconvert.Path.ToString()); }
} catch (Exception se) { } return getres; }这个是我的程序代码,求知道的人帮解决一下,因为,这个在
var gettakeres=newline.Invoke();报不能调用Null值方法
这代码贴的,用插入代码的功能吧
# Absolute path to file $file = $Result.Path; # URL no trailing "/" $url = "https://webdav.synapse.com/Disney/beakerdata" # Username and Password $user = "" $pass = "" ######################################## # Script ####################################### # Append the name of the file onto the url $url += "/" + $file.split('\')[(($file.split("\")).count - 1)] ######################################## # Begin prepare file data ####################################### #Write-Host "File upload started" # Set as binary data Set-Variable -name adFileTypeBinary -value 1 -option Constant $objADOStream = New-Object -ComObject ADODB.Stream $objADOStream.Open() $objADOStream.Type = $adFileTypeBinary $objADOStream.LoadFromFile("$file") $arrbuffer = $objADOStream.Read() ######################################## # End prepare file data ####################################### ######################################## # Begin WebDav connection and PUT ####################################### $objXMLHTTP = New-Object -ComObject MSXML2.ServerXMLHTTP.6.0 $objXMLHTTP.setOption(2,13056) # Ignore ceriticate errors $objXMLHTTP.Open("PUT", $url, $False, $user, $pass) $objXMLHTTP.send($arrbuffer) # This will give you the response text # $objXMLHTTP.responseText # Verify upload if ($objXMLHTTP.status -eq 201) { # Write-Host "File upload finished" $Result.RetMsg = "File upload finished" } elseif ($objXMLHTTP.status -eq 204) { #Write-Host = "File already exists" $Result.RetMsg ="File already exists" } # status code detail # http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms767625(v=vs.85).aspx ######################################## # End WebDav connection and PUT #######################################
/// <summary> /// C# Language Call the PowerShell Script /// Author:chenkai Date:2010年11月9日10:12:24 /// </summary> public class Program { static void Main(string[] args) { string getresult= Program.OperatorObjectShell(); Console.WriteLine(getresult); Console.Read(); } /// <summary> /// 测试C#于Shell对象操作的可操作性 /// </summary> /// <returns>返回计算结果</returns> public static string OperatorObjectShell() { string getres = string.Empty; try { //zaiShell 执行方法参数 List<string> getlist = new List<string>(); getlist.Add("Set-ExecutionPolicy RemoteSigned");//先执行启动安全策略,,使系统可以执行powershell脚本文件 string pspath = System.IO.Path.Combine(Environment.CurrentDirectory, "webdav_push.ps1"); getlist.Add(pspath); //定义一个操作的实体对象 ConvertPatameter newconvert = new ConvertPatameter { Path = "D:\text123.txt", RetMsg = "dfdf" }; if (File.Exists(pspath)) { RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace newspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); newspace.Open(); Pipeline newline = newspace.CreatePipeline(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(newspace); Command getcmd = new Command(pspath); CommandParameter newcmdpara = new CommandParameter("Result", newconvert); getcmd.Parameters.Add(newcmdpara); newline.Commands.Add(getcmd); //注入脚本一个.NEt对象 这样在powershell脚本中就可以直接通过$key访问和操作这个对象 //newspace.SessionStateProxy.SetVariable(getshellpar.ShellKey,getshellpar.ShellValue); newspace.SessionStateProxy.SetVariable("Result", newconvert); /*//执行 var gettakeres=newline.Invoke(); foreach (var getstr in gettakeres) { Console.WriteLine(getstr.ToString()); } Console.Write(newconvert.RetMsg.ToString()+newconvert.Path.ToString()); }*/ //Exec Restult var getresult = newline.Invoke(); if (getresult != null) { StringBuilder getbuilder = new StringBuilder(); foreach (var getresstr in getresult) { getbuilder.AppendLine(getresstr.ToString()); } getres = getbuilder.ToString(); } //close newspace.Close(); } } catch (Exception se) { } return getres; } }
现在在var gettakeres=newline.Invoke();报“使用“1”个参数调用“LoadFromFile”时发生异常:“文件无法被打开。”怎么解决呀,根本不懂powershell,根本不知道如何弄呀
眼晕@_@