[置顶] PowerShell的学习笔记(3)

标签: PowerShell
486人阅读 评论(0) 收藏 举报
分类:

继续学习PowerShell,继续记笔记。下面的内容可能会天马行空,不会按照一定的顺序,但是会给每个知识点一个标题。

@在Powershell命令行交互或者弹出长提交互

#1 通过Read-Host

在PowerShell中弹出对话框,至少有2中方式,在PowerShell命令行中,可以通过在ISE中输入Read-Host弹出一个对话框(在PowerShell V1和V2的在ISE弹出,PowerShell V3没有试过,在PowerShell V5中已经不弹出,且和PowerShell控制台行为一样,是命令式交互);

PS C:\Users\rodneyzhu.BEIJING> read-Host "Please enter the server name"
Please enter the server name: server-host-dsc
server-host-dsc
PS C:\Users\rodneyzhu.BEIJING>

#2 通过调用VisualBasic中方法

通过调用VisualBasic的方法也可以弹出对话框,下面的例子也可以告诉我们,如果在PowerShell中调用DLL中方法。
第一行不能缺,否则第二行执行不了。第一样主要用来加载DLL到内存。第二行才是调用其中的一个类的方法

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$computername=[Microsoft.VisualBasic.Interaction]::InputBox('Enter a computername','localhost')



@Write-Host 给输出添加颜色

Write-Host "Colorful" -ForegroundColor Yellow -BackgroundColor Magenta

@在PowerShell中使用状态条

Example 1

PS C:\>$Events = Get-EventLog -logname system
PS C:\> $Events | foreach-object -begin {clear-host;$I=0;$out=""} `
-process {if($_.message -like "*bios*") {$out=$out + $_.Message}; $I = $I+1;
Write-Progress -Activity "Searching Events" -Status "Progress:" -PercentComplete ($I/$Events.count*100)} `
-end {$out}

Example 2

PS C:\>for($I = 1; $I -lt 101; $I++ )
    {Write-Progress -Activity Updating -Status 'Progress->' -PercentComplete $I -CurrentOperation OuterLoop; `
 PS C:\> for($j = 1; $j -lt 101; $j++ )
    {Write-Progress -Id 1 -Activity Updating -Status 'Progress' - PercentComplete $j -CurrentOperation InnerLoop} }


@在PowerShell中的For循环

使用for关键字,支持在PowerShell中实现For循环
PS C:\Users\rodneyzhu.BEIJING> for($I = 1; $I -lt 10; $I++ ){Write-Output "Hello: $I"}
Hello: 1
Hello: 2
Hello: 3
Hello: 4
Hello: 5
Hello: 6
Hello: 7
Hello: 8
Hello: 9

@PowerShell中的Module

Powershell的Module是一个非常有用的概念。类似于Java的Package或者C#的namespace的概念。一个Module里面包含了很多的Function。Module的psm1, module只有部署在$env:PSModulePath这个环境变量制定的路径下才能使得模块生效。
C:\Users\<User Name>\Documents\WindowsPowerShell\Modules;
C:\ProgramFiles\WindowsPowerShell\Modules;
C:\windows\system32\WindowsPowerShell\v1.0\Modules\;

下面咱们先定制和部署一个Module试试。
模块的名字是:CurrencyConversion.psm1
其内容如下:
Function ConvertTo-GBPEuro {
param ([int]$Pounds)
 
$Currency = New-WebServiceProxy -Uri http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
$GBPEURConversionRate = $Currency.ConversionRate('USD','ZWD')
Write-Host $GBPEURConversionRate
$Euros = $Pounds * $GBPEURConversionRate
Write-Host "$Pounds British Pounds convert to $Euros Euros"



Function ConvertTo-EuroGBP  {
param ([int]$Euros)
 
$Currency = New-WebServiceProxy -Uri http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
$EURGBPConversionRate = $Currency.ConversionRate('EUR','GBP')
$Pounds = $Euros * $EURGBPConversionRate
Write-Host "$Euros Euros convert to $Pounds British Pounds"


然后把其拷贝到C:\Users\<User Name>\Documents\WindowsPowerShell\Modules\CurrencyConversion目录下。
Get-Module -ListAvailable 命令查看,其模块已经存在。
PS C:\Users\rodney> Get-Module -ListAvailable
    Directory: C:\Users\rodney\Documents\WindowsPowerShell\Modules
ModuleType Version    Name                                ExportedCommands                                                                                                           
---------- -------    ----                                ----------------                                                                                                           
Script     1.0        CurrencyConversion                  {ConvertTo-GBPEuro, ConvertTo-EuroGBP}                                                                                                                              

另外,可以通New-ModuleManifest  这个命令生成这个模块的Manifest,后缀名为:.psd1,里面有其模块的版本号,作者,
导出的信息等。具体使用的方式,请执行Help New-ModuleManifest 命令查看。下面是CurrencyConversion.psd1的一个范本。

#
# Module manifest for module 'CurrencyConversion'
#
# Generated by: Henry Zhu
#
# Generated on: 08/02/2017
#
 
@{
 
# Script module or binary module file associated with this manifest
ModuleToProcess = 'CurrencyConversion.psm1'
 
# Version number of this module.
ModuleVersion = '1.0'
 
# ID used to uniquely identify this module
GUID = 'c6f2e5e7-91ff-4924-b4bb-8db0624195c9'
 
# Author of this module
Author = 'Henry Zhu'
 
# Company or vendor of this module
CompanyName = 'Example'
 
# Copyright statement for this module
Copyright = '(c) 2011 Henry Zhu. All rights reserved.'
 
# Description of the functionality provided by this module
Description = 'Convert values between different currencies'
 
# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = ''
 
# Name of the Windows PowerShell host required by this module
PowerShellHostName = ''
 
# Minimum version of the Windows PowerShell host required by this module
PowerShellHostVersion = ''
 
# Minimum version of the .NET Framework required by this module
DotNetFrameworkVersion = ''
 
# Minimum version of the common language runtime (CLR) required by this module
CLRVersion = ''
 
# Processor architecture (None, X86, Amd64, IA64) required by this module
ProcessorArchitecture = ''
 
# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @()
 
# Assemblies that must be loaded prior to importing this module
RequiredAssemblies = @()
 
# Script files (.ps1) that are run in the caller's environment prior to importing this module
ScriptsToProcess = @()
 
# Type files (.ps1xml) to be loaded when importing this module
TypesToProcess = @()
 
# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = @()
 
# Modules to import as nested modules of the module specified in ModuleToProcess
NestedModules = @()
 
# Functions to export from this module
FunctionsToExport = '*'
 
# Cmdlets to export from this module
CmdletsToExport = '*'
 
# Variables to export from this module
VariablesToExport = '*'
 
# Aliases to export from this module
AliasesToExport = '*'
 
# List of all modules packaged with this module
ModuleList = @()
 
# List of all files packaged with this module
FileList = 'CurrencyConversion.psm1'
 
# Private data to pass to the module specified in ModuleToProcess
PrivateData = ''
 
}


入门参考文献:
https://www.simple-talk.com/sysadmin/powershell/an-introduction-to-powershell-modules/



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:385978次
    • 积分:6349
    • 等级:
    • 排名:第4081名
    • 原创:228篇
    • 转载:0篇
    • 译文:5篇
    • 评论:157条
    博客专栏