挖土

Coding for fun.
posts - 29, comments - 20, trackbacks - 0, articles - 23
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

2010年12月20日

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName String %USERNAME&
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword String %PASSWORD% or blank
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon DWORD 1

 

posted @ 2010-12-20 15:56 挖土. 阅读(24) 评论(0) 编辑

2010年12月8日

DotNet Framework 4.0 发布以后,你在 PowerShell 中执行引用 4.0 的 exe、dll,会报错,说你的Powershell的 dotnet framework 版本和你引用的 assemblis 不一致。这是因为默认PowerShell 的 Notdet FrameWork 版本是 v2.0, 我们需要让 PowserShell 支持 v4.0。


你只要创建一个 Powershell.exe.config 文件,在以下两个目录,就可以了。

  • C:\Windows\System32\WindowsPowerShell\v1.0
  • C:\Windows\SysWOW64\WindowsPowerShell\v1.0
1 <?xml version="1.0"?>
2 <configuration>
3     <startup useLegacyV2RuntimeActivationPolicy="true">
4         <supportedRuntime version="v4.0.30319"/>
5         <supportedRuntime version="v2.0.50727"/>
6     </startup>
7 </configuration>

 

 

 另外,也可以更改注册表, 把 RuntimeVersionv2.0.50727 改成  v4.0.30319。

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine

 

 

posted @ 2010-12-08 10:37 挖土. 阅读(59) 评论(0) 编辑

在微软的技术论坛上看到这个脚本,感觉自己的PowerShell 理念,还有很多不熟悉,好好研究了一下这个脚本,记录在自己的Blog上。


怎样同时为网络中很多电脑安装Dotnet Framework 4.0。

 

 1 $Hosts= Get-Content -Path "C:\hosts.txt"
 2 [ScriptBlock] $script = {
 3     #$ErrorActionPreference = "Stop"
 4     $MachineName= gc env:computername
 5     C:\Windows\System32\net.exe use "\\RemoteMachine\SharedFolder\DotnetFrameWork 4.0" password /user:domain\username 
 6     if(!(Test-Path "C:\Temp"))
 7     {
 8         New-Item -Path "C:\Temp" -ItemType directory
 9     }
10     Copy-Item -Path "\\RemoteMachine\SharedFolder\DotnetFrameWork 4.0\dotNetFx40_Full_x86_x64.exe" -Destination "C:\Temp" -Force -Recurse    
11     function LaunchProcessAndWait([string] $FileName, [string] $CommandLineArgs)
12     {
13         #preferences
14         $ErrorActionPreference="Stop"
15         try
16         {
17             Write-Host "Starting process $FileName on machine $($MachineName)"
18             $p = Start-Process -FilePath $FileName -ArgumentList $CommandLineArgs -Wait -PassThru
19             Write-Host "The process $FileName exit code is $($p.exitcode)"
20             return $p.ExitCode            
21         }
22         catch
23         {
24             Write-Host "Error launching process $FileName"
25             throw
26         }
27     }
28     LaunchProcessAndWait "C:\Temp\dotNetFx40_Full_x86_x64.exe" "/I /q"
29 }
30 $cred = Get-Credential
31 $sessions = $Hosts | New-PSSession -Credential $cred
32 Invoke-Command -ScriptBlock $script -Session $sessions
33 $sessions | Remove-PSSession

 

 

另外的解决方法: 

-CredSSP 非常重要. 否则安装会被远程机的 UNC 阻止而失败,需要为所有的远程机启用 CredSSP. This works only from Vista SP1 onwards since CredSSP is not available on XP SP3.

 

代码
1 $cred = Get-Credential
2 Invoke-Command -ComputerName "Server01" -ScriptBlock { Start-Process -FilePath "\\Staging\Share\dotNetFx40_Full_x86_x64.exe" -ArgumentsList "/q /norestart" } -CredSSP -Crdential $cred

 

 

 

posted @ 2010-12-08 10:09 挖土. 阅读(38) 评论(0) 编辑

2010年8月9日

调用.netFramework中的静态函数非常方便,可以不用去创建一个新的instance。下面调用数据函数Tan的例子。

 

PS C:\> [system.math]::tan(45

 

 

也可以这样写:

 

PS C:\> [math]::tan(45

 

 

日期函数调用示例:

 

PS C:\> $dtNow = [System.DateTime]::Now
PS C:\
> $dtNow
PS C:\
> $deNow = [DateTime]::Now
PS C:\
> $deNow 

 

 

 

posted @ 2010-08-09 16:57 挖土. 阅读(87) 评论(0) 编辑

2010年7月30日

最近几天一直在学习PowerShell,一直用Notepad在编辑脚本语言,那是一个很痛苦的黑白界面。好在家里可以用Editplus,那还有一点颜色来帮助你。

 

今天终于在Windows PowerShell Blog 上看到一篇博文,PowerShell Integration Into Visual Studio 这让我我很是惊喜,迫不及待的想要安装了。

安装之前,首先要安装

 

PowerGUI VSX is an extension for Visual Studio that adds PowerGUI’s editor with Intellisense, syntax highlighting and snippets for PowerShell script files to Visual Studio!  This can make it much easier to create PowerShell scripts or modules if you’re already working inside Visual Studio.

 

 

 

posted @ 2010-07-30 16:30 挖土. 阅读(32) 评论(0) 编辑

泛型用起来很方便,在PowerShell 2.0的版本中,泛型的定义方法也很简单。下面看看List的使用方法:

1 $foo = New-Object 'System.Collections.Generic.List[int]'
2 $foo.Add(10)
3 $foo.Add(20)
4 $foo.Add(30)
5 $foo
6 
7 #使用Get-Member查看$foo的方法和属性
8 Get-Member -InputObject $foo

  

如果是需要两个参数的Dictionary:

 

 1 $foo = New-Object 'System.Collections.Generic.Dictionary[string,string]'
 2 $foo.Add('FOO','BAR')
 3 $foo.Add('FOB','MENU')
 4 $foo.Add('FOC','MOUSE')
 5 $foo.('FOO')
 6 $foo.Item('FOO')
 7 $foo
 8 
 9 #使用Get-Member查看$foo的方法和属性
10 Get-Member -InputObject $foo

 

 为了方便定义泛型的变量,可以建立一些方法,使用起来就会非常简单。

 

 1 # 创建List变量
 2 Function global:New-GenericList([type] $type)
 3 {
 4     $base = [System.Collections.Generic.List``1]
 5     $qt = $base.MakeGenericType(@($type))
 6     New-Object $qt
 7 }
 8 
 9 # 创建Dictionary变量
10 Function global:New-GenericDictionary([type] $keyType, [type] $valueType)
11 {
12     $base = [System.Collections.Generic.Dictionary``2]
13     $qc = $base.MakeGenericType(($keyType$valueType))
14     New-Object $qc
15 }

 

这样子使用起来就非常简单了:

 

 1 PS D:\> $intList = New-GenericList int
 2 PS D:\> $intList.Add(10)
 3 PS D:\> $intList.Add(20)
 4 PS D:\> $intList.Add(30)
 5 PS D:\> $intList
 6 10
 7 20
 8 30
 9 
10 PS D:\> $gd = New-GenericDictionary string int
11 PS D:\> $gd["Red"= 1
12 PS D:\> $gd["Blue"= 2
13 PS D:\> $gd
14 
15 Key          Value
16 ---          -----
17 Red              1
18 Blue             2

posted @ 2010-07-30 11:27 挖土. 阅读(81) 评论(0) 编辑

2010年7月29日

如果DLL是在Dotnet Framework基础上,Visual Studio编译出来的,可以按照如下方法在PowerShell中引用。

 

 

 1 #引入DLL
 2 [System.Reflection.Assembly]::LoadFrom('D:\Test\MCF.Infrastructure.DTS.dll') | Out-null
 3 [System.Reflection.Assembly]::LoadFrom('D:\MCF\MCF.Infra.dll') | Out-null
 4 
 5 #创建对象
 6 $log = new-object MCF.Infra.Logger
 7 $lib = new-object MCF.Infrastructure.DTS.DTSClientLib($log)
 8  
 9 #调用类库的方法
10 $status = $lib.GetJobStatus() 

 

 

因为在创建 DTSClientLib 对象时,可以传入 null,可以这样

 

1 #引入DLL
2 [System.Reflection.Assembly]::LoadFrom('D:\Test\MCF.Infrastructure.DTS.dll') | Out-null
3 
4 #创建对象
5 $lib = new-object MCF.Infrastructure.DTS.DTSClientLib @($null)
6 
7 #调用类库的方法
8 $status = $lib.GetJobStatus() 

 

 

   

posted @ 2010-07-29 15:41 挖土. 阅读(86) 评论(0) 编辑

Add-Content 输出 ASCII 格式

Out-File 默认输出 Unicode 格式, 可以使用 -Encoding ASCII来指定编码方式。

执行以下命令,然后查看三个文件的格式。

 

1 Add-Content "D:\testA.txt" "ASCII Format Only"
2 
3 Out-File -FilePath "D:\TestB.txt" -Append -InputObject "Default, file format is Unicode"
4 
5 Out-File -FilePath "D:\TestC.txt" -Append -Encoding ASCII -InputObject "Now, file format is ASCII." 

 

posted @ 2010-07-29 15:24 挖土. 阅读(76) 评论(0) 编辑

2010年3月31日

摘要: 代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->publicstaticclassStringExtend{publicstaticboolIsNullOrEmpty(thisstrings){returnstring.IsNullOrEmpty(...阅读全文

posted @ 2010-03-31 14:58 挖土. 阅读(121) 评论(3) 编辑

2010年3月19日

摘要: Tools->Envirmonent Options->Preferences->不选定 Backgroun compilationTools->Envirmonent Options->Preferences->选定 CacheHeaders on startupProject -> Options ->Advanced Compile->I...阅读全文

posted @ 2010-03-19 00:07 挖土. 阅读(81) 评论(0) 编辑