PowerShell 学习笔记

最近学习 PowerShell 遇到了一些问题,现记录如下:

 

1、 当引用第三方dll  时,需先在 powershell 中加入如下:

[void][reflection.assembly]::LoadFile("G:/MyAssembly.dll")   #此处为dll 路径
[Math.methods]::MyMethod(“Hello”)  #
调用静态方法必须用 ”::”
$a=New-Object Math.Methods  #
创建dll 中自定义Class对象
$a.MyMethodCommon(2,3)  #
调用普通public 方法,即用 “.” 即可

 

2、  当创建 .Net 自带命名空间下的 Class 对象时,需如下:

$appPool = New-Object System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/AppPools");#创建应用程序池对象

3、  当需要调用powershell 文件远程控制远程机器时,需在本机和远程机做如下更改:

Set-ExecutionPolicy RemoteSigned

Enable-PSRemoting

4、  创建 Com 对象 控制声音读取文本等

#$Voice = new-object -com SAPI.SpVoice;

#$Voice.Speak('Test',1);

#$Voice.Speak('D:/zhao/tools.txt',5);

#$Voice.Pause();

        5C# 中的 true,false,null 对应 powershell 中的 $true,$false,$null等,.Net 中的 this 在 powershell 中为"$_"

6、  创建远程登陆信息,并启动 sharepoint 2010 服务中的  doctrans 服务

 # escape and secure the password

$securePassword = ConvertTo-SecureString $password.replace('`','``').replace("$","`$") -AsPlainText -Force

# Create a PSCredential for remote invoke

$credential = new-object Management.Automation.PSCredential(($domain+"/"+$userName), $securePassword)

# call remote server

invoke-command -ComputerName $computer -Credential $credential -ScriptBLock{

                Param(

                                #[System.Xml.XmlDocument]$requestXML,

                                #[string]$url,

                                #[string]$sharedDocumentsFolder,

                                #[string]$domain,

                                [string]$versionMOSS

                )

               

                $resultXMLString="false"

                if($versionMOSS -eq "MOSS2007")

                {

                                stop-service -name "Office Document Conversions Launcher Service"

                                stop-service -name "Office Document Conversions Load Balancer Service" 

                                start-service -name "Office Document Conversions Load Balancer Service"

                                start-service -name "Office Document Conversions Launcher Service"

    }

    elseif($versionMOSS -eq "MOSS2010")

    {

                                restart-service -name "Document Conversions Load Balancer for Microsoft SharePoint Server 2010"

                                restart-service -name "Document Conversions Launcher for Microsoft SharePoint Server 2010"

                }              

} -argumentlist $requestXML,$url,$sharedDocumentsFolder,$domain,$versionMOSS

 

#if(!$?){throw $error[0]}

 

    7  采用  powershell 启动、停止、回收 IIS  应用程序池资源等,当回收资源时需防备如果应用程序停止则会抛出异常,具体如下:

try

{

  $appPool = New-Object   System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/AppPools");

   $strMethod = "Recycle";#"Recycle";#Stop;#Start

   $dircEntry = $appPool.Children;

   foreach($childEntry in $dircEntry)

   {

     $childEntry.Invoke($strMethod,$null);

     $childEntry.CommitChanges();

     $childEntry.Close();

   }

}

catch{"Error";}        

 

你可能感兴趣的:(powershell,sharepoint,microsoft,service,office,string)