Powershell DSC 自帶了一些常見的資源。如果需要新的,可以下載安裝 DSC Resource Kit 10或者從Powershell Gallery下載。
10年積累的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有前進(jìn)免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
比如說查看當(dāng)前已經(jīng)安裝了的資源
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文件已經(jīng)拷貝了
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
該文件已經(jīng)解壓到指定目錄
3. 創(chuàng)建環(huán)境變量 資源叫做Environment
configuration TestEnvVar { Node sydittest { Environment NewVar{ Name = 'MYNEWVAR' Value = 'Value to store' Ensure = 'Present' } } } TestEnvVar -OutputPath c:\DSC\Mod5Config
確認(rèn)該環(huán)境變量已經(jīng)創(chuàng)建
4.創(chuàng)建本地用戶 資源是User
這個(gè)是幾個(gè)資源里面相對麻煩的一個(gè),因?yàn)槟J(rèn)傳遞的是明文,他不會允許你推送的,我需要明確告訴他允許明文。
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
可以看見是明文發(fā)送的,如果是生產(chǎn)環(huán)境,需要用證書加密的才行。
推送出去
Start-DscConfiguration -computername sydittest -Path c:\dsc\Mod5Config -Wait -Verbose -force
可以看見用戶已經(jīng)創(chuàng)建了
5.創(chuàng)建本地組 資源 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.創(chuàng)建注冊表鍵 資源 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
確認(rèn)成功創(chuàng)建
7. 創(chuàng)建進(jìn)程(運(yùn)行某個(gè)程序) 資源是 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
確認(rèn)
8. 更改服務(wù)的狀態(tài)
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. 腳本資源
這個(gè)是當(dāng)現(xiàn)有的資源都無法滿足需求的時(shí)候,可以進(jìn)行自定義資源。里面有3個(gè)子模塊,test,set,get
首先通過 test 判斷狀態(tài),返回一個(gè)boolean值,如果是true,忽略;如果是false,那么在set里面進(jìn)行處理;相當(dāng)于于一個(gè)if else的判斷語句。get可以通過getdsc的命令獲取當(dāng)前狀態(tài)
下面的例子是判斷如果bits服務(wù)開啟,那么關(guān)閉
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.rwnh.cn/article0/gjhiio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、品牌網(wǎng)站制作、全網(wǎng)營銷推廣、品牌網(wǎng)站設(shè)計(jì)、、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)