Powershell DSC 自帶了一些常見的資源。如果需要新的,可以下載安裝 DSC Resource Kit 10或者從Powershell Gallery下載。
10年積累的成都網站設計、成都網站建設、外貿網站建設經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先網站設計后付款的網站建設流程,更有前進免費網站建設讓你可以放心的選擇與我們合作。
比如說查看當前已經安裝了的資源
PS C:\Windows\system32> Get-DscResource ImplementedAs Name ModuleName Version Properties ------------- ---- ---------- ------- ---------- Binary File {DestinationPath, Attributes, Checksum, Content... PowerShell Archive PSDesiredStateConfiguration 1.1 {Destination, Path, Checksum, Credential...} PowerShell Environment PSDesiredStateConfiguration 1.1 {Name, DependsOn, Ensure, Path...} PowerShell Group PSDesiredStateConfiguration 1.1 {GroupName, Credential, DependsOn, Description...} Binary Log PSDesiredStateConfiguration 1.1 {Message, DependsOn, PsDscRunAsCredential} PowerShell Package PSDesiredStateConfiguration 1.1 {Name, Path, ProductId, Arguments...} PowerShell Registry PSDesiredStateConfiguration 1.1 {Key, ValueName, DependsOn, Ensure...} PowerShell Script PSDesiredStateConfiguration 1.1 {GetScript, SetScript, TestScript, Credential...} PowerShell Service PSDesiredStateConfiguration 1.1 {Name, BuiltInAccount, Credential, Dependencies... PowerShell User PSDesiredStateConfiguration 1.1 {UserName, DependsOn, Description, Disabled...} PowerShell WaitForAll PSDesiredStateConfiguration 1.1 {NodeName, ResourceName, DependsOn, PsDscRunAsC... PowerShell WaitForAny PSDesiredStateConfiguration 1.1 {NodeName, ResourceName, DependsOn, PsDscRunAsC... PowerShell WaitForSome PSDesiredStateConfiguration 1.1 {NodeCount, NodeName, ResourceName, DependsOn...} PowerShell WindowsFeature PSDesiredStateConfiguration 1.1 {Name, Credential, DependsOn, Ensure...} PowerShell WindowsOptionalFeature PSDesiredStateConfiguration 1.1 {Name, DependsOn, Ensure, LogLevel...} PowerShell WindowsProcess PSDesiredStateConfiguration 1.1 {Arguments, Path, Credential, DependsOn...} PowerShell xArchive xPSDesiredStateConfiguration 3.5.0.0 {Destination, Path, CompressionLevel, DependsOn... PowerShell xDSCWebService xPSDesiredStateConfiguration 3.5.0.0 {CertificateThumbPrint, EndpointName, AcceptSel... PowerShell xGroup xPSDesiredStateConfiguration 3.5.0.0 {GroupName, Credential, DependsOn, Description...} PowerShell xPackage xPSDesiredStateConfiguration 3.5.0.0 {Name, Path, ProductId, Arguments...} PowerShell xPSEndpoint xPSDesiredStateConfiguration 3.5.0.0 {Name, AccessMode, DependsOn, Ensure...} PowerShell xRemoteFile xPSDesiredStateConfiguration 3.5.0.0 {DestinationPath, Uri, Credential, DependsOn...} PowerShell xService xPSDesiredStateConfiguration 3.5.0.0 {Name, BuiltInAccount, Credential, Dependencies... PowerShell xWindowsOptionalFeature xPSDesiredStateConfiguration 3.5.0.0 {Name, DependsOn, Ensure, LogLevel...} PowerShell xWindowsProcess xPSDesiredStateConfiguration 3.5.0.0 {Arguments, Path, Credential, DependsOn...}
下面是一些常見的例子:
拷貝文件 資源叫做 File
configuration TestFile { Node sydittest { File ZipFile { Ensure = "Present" Type = "Directory“ # Default is “File” Force = $True Recurse = $True SourcePath = '\\sydit01\test' DestinationPath = 'C:\Downloads' # On Sydittest } } } TestFile -OutputPath c:\DSC\Mod5Config Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force
查看該zip文件已經拷貝了
2.解壓zip文件,資源叫做 Archive
configuration TestArchive { Node sydittest { Archive Unzip{ Destination = 'C:\unzip' Path = 'c:\downloads\temp.zip' Checksum = 'SHA-256' Validate = $true Force = $true Ensure = 'Present' } } } TestArchive -OutputPath c:\DSC\Mod5Config Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -Force
該文件已經解壓到指定目錄
3. 創建環境變量 資源叫做Environment
configuration TestEnvVar { Node sydittest { Environment NewVar{ Name = 'MYNEWVAR' Value = 'Value to store' Ensure = 'Present' } } } TestEnvVar -OutputPath c:\DSC\Mod5Config
確認該環境變量已經創建
4.創建本地用戶 資源是User
這個是幾個資源里面相對麻煩的一個,因為默認傳遞的是明文,他不會允許你推送的,我需要明確告訴他允許明文。
configuration TestUser { param ( [PSCredential]$Credential ) node sydittest { User TestUser { UserName = $Credential.UserName Ensure = 'Present' Password = $Credential Description = 'User created by DSC' PasswordNeverExpires = $true PasswordChangeNotAllowed = $true } } } $ConfigData = @{ AllNodes = @( @{ NodeName = 'sydittest' PSDscAllowPlainTextPassword=$true } ) } TestUser -ConfigurationData $ConfigData -Credential (Get-Credential) -OutputPath c:\DSC\Mod5Config
可以看見是明文發送的,如果是生產環境,需要用證書加密的才行。
推送出去
Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force
可以看見用戶已經創建了
5.創建本地組 資源 Group
configuration TestGroup { Node sydittest { Group CreateGroup { Ensure = 'Present' GroupName = 'TestGroup' Description = 'This is a DSC test group' Members = 'administrator' } } } TestGroup -OutputPath c:\DSC\Mod5Config Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force
6.創建注冊表鍵 資源 Registry
configuration TestRegistry { Node sydittest { Registry CreateReg { Key = 'HKEY_Local_Machine\Software\DSCTest' ValueName = 'DSCTestGood' ValueType = 'string' ValueData = 'True' } } } Testregistry -OutputPath c:\DSC\Mod5Config Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force
確認成功創建
7. 創建進程(運行某個程序) 資源是 Windowsprocess
configuration TestProcess { Node sydittest { WindowsProcess CreateNotepad { Path = 'notepad.exe' Arguments = '' } WindowsProcess CreatePaint { Path = 'mspaint.exe' Arguments = '' } } } TestProcess -OutputPath c:\DSC\Mod5Config Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force
確認
8. 更改服務的狀態
configuration TestService { Node sydittest { Service StartAudio { Name = 'Audiosrv' State = 'Running' StartupType = 'Automatic' } } } TestService -OutputPath c:\DSC\Mod5Config Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force
更改前
更改
更改后
9. 腳本資源
這個是當現有的資源都無法滿足需求的時候,可以進行自定義資源。里面有3個子模塊,test,set,get
首先通過 test 判斷狀態,返回一個boolean值,如果是true,忽略;如果是false,那么在set里面進行處理;相當于于一個if else的判斷語句。get可以通過getdsc的命令獲取當前狀態
下面的例子是判斷如果bits服務開啟,那么關閉
configuration TestScript { Node sydittest { Script TestScript { GetScript = { @{ GetScript = $GetScript SetScript = $setScript TestScript = $TestScript Result = (Get-Service -name bits).status } } SetScript = { stop-Service -name bits } TestScript = { $Status=(Get-service -name bits).status $Status -eq 'stopped' } } } } Testscript -OutputPath c:\DSC\Mod5Config Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force
分享名稱:PowershellDSC5.0-資源的使用
文章源于:http://www.yijiale78.com/article0/gjhiio.html
成都網站建設公司_創新互聯,為您提供服務器托管、品牌網站制作、全網營銷推廣、品牌網站設計、、Google
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯