Tämä näyttää erot valitun ja nykyisen version kesken tästä sivusta.
Both sides previous revision Edellinen revisio Seuraava revisio | Edellinen revisio | ||
ohjeet:skriptit [2010/10/13 11:23] mikko |
ohjeet:skriptit [2015/09/21 08:46] (nykyinen) matronka [Luo package ja programsit Configuration Manageriin] |
||
---|---|---|---|
Rivi 5: | Rivi 5: | ||
==== Onko koneelle kirjautunut käyttäjä domain-käyttäjä ==== | ==== Onko koneelle kirjautunut käyttäjä domain-käyttäjä ==== | ||
+ | Seuraava VBScript-koodinpätkä hakee koneelle kirjautuneen käyttäjän tiedot ympäristömuuttujista ja tarkistaa onko käyttäjä domain-tunnus vaiko ei. | ||
+ | <code vb> | ||
+ | Set objShell = CreateObject("WScript.Shell") | ||
+ | Set objProcEnv = objShell.Environment("Process") | ||
+ | strUserName = objProcEnv("USERNAME") | ||
+ | strUserDomain = objProcEnv("USERDOMAIN") | ||
+ | strUserDNSDomain = objProcEnv("USERDNSDOMAIN") | ||
+ | If strUserDNSDomain <> "UTU.FI" Then | ||
+ | strValue = "Koneelle kirjautunut käyttäjä " & VbCrLf & VbCrLf & strUserDomain & "\" & strUserName & VbCrLf & VbCrLf & "ei ole utu-domainkäyttäjä." | ||
+ | answer = MsgBox(strValue, vbOKOnly + vbCritical + vbSystemModal, "Huomio!") | ||
+ | WScript.Quit ' palautetaan -10, jos ei ole utu-domainin käyttäjä | ||
+ | </code> | ||
+ | |||
+ | ==== Onko prosessi käynnissä ==== | ||
+ | |||
+ | VBSkript-funktio, joka käyttää WMI-rajapintaa selvittämään onko koneella tietyn niminen prosessi käynnissä. | ||
+ | <code vb> | ||
+ | Function isProcessRunning(strIn) | ||
+ | |||
+ | Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") | ||
+ | Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & strIn & "'") | ||
+ | |||
+ | If colProcesses.Count > 0 Then | ||
+ | isProcessRunning = True | ||
+ | Else | ||
+ | isProcessRunning = False | ||
+ | End If | ||
+ | End Function | ||
+ | </code> | ||
- | ==== x86-ympäristö x64-käyttöjärjestelmässä ==== | + | ==== x86 vaiko x64 ==== |
Seuraavalla VBScript-funktiolla saat selville käyttöjärjestelmän bittisyyden | Seuraavalla VBScript-funktiolla saat selville käyttöjärjestelmän bittisyyden | ||
Rivi 14: | Rivi 43: | ||
64-bittisessä ympäristössä aidosti 64-bittisessä Windowsissa. | 64-bittisessä ympäristössä aidosti 64-bittisessä Windowsissa. | ||
- | <code> | + | <code vb> |
' Finds out Real Operating System architecture (despite the running process image type) | ' Finds out Real Operating System architecture (despite the running process image type) | ||
' Returns: 0: OS architecture not found (or propably Intel Itanium) | ' Returns: 0: OS architecture not found (or propably Intel Itanium) | ||
Rivi 41: | Rivi 70: | ||
End Function | End Function | ||
+ | </code> | ||
+ | ==== x64 - registryn käsittely ==== | ||
+ | Seuraavat VBScript-funktiot (**EnumRegKey**, **ReadRegStr**, **CreateRegKey**, **CreateRegStringValue**) mahdollistavat registryn Key ja REG_SZ -arvojen käsittelyn WoW64-rajapinnan ohi. | ||
+ | |||
+ | <code vb> | ||
+ | ' --- Enumerates registry keys from the local computer's registry using WMI. | ||
+ | ' Parameters: | ||
+ | ' RootKey - The registry hive (see http://msdn.microsoft.com/en-us/library/aa390788(VS.85).aspx for a list of possible values). | ||
+ | ' Key - The key to be enumerated. | ||
+ | ' RegType - The registry bitness: 32 or 64. | ||
+ | ' Returns: | ||
+ | ' Null if Key was not found, otherwise Variable Array with Keys as strings | ||
+ | |||
+ | Function EnumRegKey (RootKey, Key, RegType) | ||
+ | Dim objCtx, objLocator, objReg, objInParams, objOutParams | ||
+ | | ||
+ | Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet") | ||
+ | objCtx.Add "__ProviderArchitecture", RegType | ||
+ | objCtx.Add "__RequiredArchitecture", TRUE | ||
+ | |||
+ | Set objLocator = CreateObject("Wbemscripting.SWbemLocator") | ||
+ | Set objReg = objLocator.ConnectServer("", "root\default", "", "", , , , objCtx).Get("StdRegProv") | ||
+ | |||
+ | Set objInParams = objReg.Methods_("EnumKey").InParameters | ||
+ | objInParams.hDefKey = RootKey | ||
+ | objInParams.sSubKeyName = Key | ||
+ | |||
+ | Set objOutParams = objReg.ExecMethod_("EnumKey", objInParams, , objCtx) | ||
+ | |||
+ | EnumRegKey = objOutParams.sNames | ||
+ | End Function | ||
+ | |||
+ | |||
+ | ' --- Reads a REG_SZ value from the local computer's registry using WMI. | ||
+ | ' Parameters: | ||
+ | ' RootKey - The registry hive (see http://msdn.microsoft.com/en-us/library/aa390788(VS.85).aspx for a list of possible values). | ||
+ | ' Key - The key that contains the desired value. | ||
+ | ' Value - The value that you want to get. | ||
+ | ' RegType - The registry bitness: 32 or 64. | ||
+ | ' Returns: | ||
+ | ' Null if Key was not found, otherwise Value as a string | ||
+ | |||
+ | Function ReadRegStr (RootKey, Key, Value, RegType) | ||
+ | Dim objCtx, objLocator, objReg, objInParams, objOutParams | ||
+ | |||
+ | Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet") | ||
+ | objCtx.Add "__ProviderArchitecture", RegType | ||
+ | objCtx.Add "__RequiredArchitecture", TRUE | ||
+ | |||
+ | Set objLocator = CreateObject("Wbemscripting.SWbemLocator") | ||
+ | Set objReg = objLocator.ConnectServer("", "root\default", "", "", , , , objCtx).Get("StdRegProv") | ||
+ | |||
+ | Set objInParams = objReg.Methods_("GetStringValue").InParameters | ||
+ | objInParams.hDefKey = RootKey | ||
+ | objInParams.sSubKeyName = Key | ||
+ | objInParams.sValueName = Value | ||
+ | |||
+ | Set objOutParams = objReg.ExecMethod_("GetStringValue", objInParams, , objCtx) | ||
+ | |||
+ | ReadRegStr = objOutParams.sValue | ||
+ | |||
+ | End Function | ||
+ | |||
+ | ' --- Creates registry key to the local computer's registry using WMI. | ||
+ | ' Parameters: | ||
+ | ' RootKey - The registry hive (see http://msdn.microsoft.com/en-us/library/aa390788(VS.85).aspx for a list of possible values). | ||
+ | ' Key - The key to be created. | ||
+ | ' RegType - The registry bitness: 32 or 64. | ||
+ | ' Returns: | ||
+ | ' Nothing | ||
+ | |||
+ | Function CreateRegKey (RootKey, Key, RegType) | ||
+ | Dim objCtx, objLocator, objReg, objInParams | ||
+ | | ||
+ | Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet") | ||
+ | objCtx.Add "__ProviderArchitecture", RegType | ||
+ | objCtx.Add "__RequiredArchitecture", TRUE | ||
+ | |||
+ | Set objLocator = CreateObject("Wbemscripting.SWbemLocator") | ||
+ | Set objReg = objLocator.ConnectServer("", "root\default", "", "", , , , objCtx).Get("StdRegProv") | ||
+ | |||
+ | Set objInParams = objReg.Methods_("CreateKey").InParameters | ||
+ | objInParams.hDefKey = RootKey | ||
+ | objInParams.sSubKeyName = Key | ||
+ | |||
+ | objReg.ExecMethod_ "CreateKey", objInParams, , objCtx | ||
+ | |||
+ | End Function | ||
+ | |||
+ | |||
+ | ' --- Creates a REG_SZ type registry Key on value to the local computer's registry using WMI. | ||
+ | ' Parameters: | ||
+ | ' RootKey - The registry hive (see http://msdn.microsoft.com/en-us/library/aa390788(VS.85).aspx for a list of possible values). | ||
+ | ' Key - The REG_SZ Key Name to be created. | ||
+ | ' Name - Name on the regisry entry | ||
+ | ' Value - Value | ||
+ | ' RegType - The registry bitness: 32 or 64. | ||
+ | ' Returns: | ||
+ | ' Nothing | ||
+ | |||
+ | Function CreateRegStringValue (RootKey, Key, Name, Value, RegType) | ||
+ | Dim objCtx, objLocator, objReg, objInParams | ||
+ | | ||
+ | Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet") | ||
+ | objCtx.Add "__ProviderArchitecture", RegType | ||
+ | objCtx.Add "__RequiredArchitecture", TRUE | ||
+ | |||
+ | Set objLocator = CreateObject("Wbemscripting.SWbemLocator") | ||
+ | Set objReg = objLocator.ConnectServer("", "root\default", "", "", , , , objCtx).Get("StdRegProv") | ||
+ | |||
+ | Set objInParams = objReg.Methods_("SetStringValue").InParameters | ||
+ | objInParams.hDefKey = RootKey | ||
+ | objInParams.sSubKeyName = Key | ||
+ | objInParams.sValueName = Name | ||
+ | objInParams.sValue = Value | ||
+ | | ||
+ | objReg.ExecMethod_ "SetStringValue", objInParams, , objCtx | ||
+ | |||
+ | End Function | ||
</code> | </code> | ||
+ | |||
+ | ==== Onko ohjelman tietty versio asennettuna? ==== | ||
+ | <code vb> | ||
+ | Option Explicit | ||
+ | Dim oShell | ||
+ | Dim regPath | ||
+ | Dim version | ||
+ | |||
+ | Set oShell = CreateObject("Wscript.Shell") | ||
+ | |||
+ | Select Case GetOSArchitecture | ||
+ | Case 32 ' x86 | ||
+ | RegPath = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{26A24AE4-039D-4CA4-87B4-2F83216035FF}\DisplayVersion" | ||
+ | Case Else ' x64 | ||
+ | RegPath = "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{26A24AE4-039D-4CA4-87B4-2F83216035FF}\DisplayVersion" | ||
+ | End Select | ||
+ | |||
+ | On Error Resume Next | ||
+ | version = oShell.RegRead(RegPath) | ||
+ | On Error Goto 0 | ||
+ | |||
+ | If version = "6.0.350" Then | ||
+ | wScript.Echo("Asennettuna") | ||
+ | End If | ||
+ | | ||
+ | wScript.Quit | ||
+ | </code> | ||
+ | |||
+ | ==== Luo package ja programsit Configuration Manageriin ==== | ||
+ | <code powershell> | ||
+ | |||
+ | #Set the program to be run only on x64 systems. | ||
+ | Function SetOperatingSystemsx64 { | ||
+ | $Program = $Args[0] | ||
+ | |||
+ | $OSystemsx64 = @(@("6.10.9999.9999","6.10.0000.0"),` | ||
+ | @("6.20.9999.9999","6.20.0000.0"),` | ||
+ | @("6.30.9999.9999","6.30.0000.0"),` | ||
+ | @("5.20.9999.9999","5.20.0000.0"),` | ||
+ | @("5.20.3790.2","5.20.3790.0"),` | ||
+ | @("6.00.9999.9999","6.00.0000.1"),` | ||
+ | @("6.10.9999.9998","6.10.0000.0"),` | ||
+ | @("6.30.9999.9998","6.30.0000.0"),` | ||
+ | @("6.20.9999.9998","6.20.0000.0"),` | ||
+ | @("6.00.9999.9999","6.00.0000.0"),` | ||
+ | @("5.20.9999.9999","5.20.3790.0"),` | ||
+ | @("10.00.9999.9999","10.00.0000.0")) | ||
+ | |||
+ | $OS_Details = @() | ||
+ | |||
+ | ForEach ($Plat in $OSystemsx64) { | ||
+ | $NewOS = ([wmiclass]"\\configmgr.utu.fi\root\sms\site_TUY:SMS_OS_Details").CreateInstance() | ||
+ | $NewOS.MaxVersion = $Plat[0] | ||
+ | $NewOS.MinVersion = $Plat[1] | ||
+ | $NewOS.Name = "Win NT" | ||
+ | $NewOS.PlatForm = "x64" | ||
+ | $NewOS *>$Null | ||
+ | |||
+ | $OS_Details = $OS_Details + $NewOS | ||
+ | } | ||
+ | |||
+ | $Program.Get() | ||
+ | $Program.SupportedOperatingSystems = $OS_Details | ||
+ | $Program.Put() | ||
+ | } | ||
+ | |||
+ | #Set the program to be run only on x86 systems. | ||
+ | Function SetOperatingSystemsx86 { | ||
+ | $Program = $Args[0] | ||
+ | |||
+ | $OSystemsx86 = @(@("6.10.9999.9999","6.10.0000.0"),` | ||
+ | @("6.20.9999.9999","6.20.0000.0"),` | ||
+ | @("6.30.9999.9999","6.30.0000.0"),` | ||
+ | @("5.20.9999.9999","5.20.0000.0"),` | ||
+ | @("5.20.3790.2","5.20.3790.0"),` | ||
+ | @("6.00.9999.9999","6.00.0000.1"),` | ||
+ | @("6.00.9999.9999","6.00.0000.0"),` | ||
+ | @("5.10.9999.9999","5.10.0000.0"),` | ||
+ | @("10.00.9999.9999","10.00.0000.0")) | ||
+ | |||
+ | $OS_Details = @() | ||
+ | |||
+ | ForEach ($Plat in $OSystemsx86) { | ||
+ | $NewOS = ([wmiclass]"\\configmgr.utu.fi\root\sms\site_TUY:SMS_OS_Details").CreateInstance() | ||
+ | $NewOS.MaxVersion = $Plat[0] | ||
+ | $NewOS.MinVersion = $Plat[1] | ||
+ | $NewOS.Name = "Win NT" | ||
+ | $NewOS.PlatForm = "I386" | ||
+ | $NewOS *>$Null | ||
+ | |||
+ | $OS_Details = $OS_Details + $NewOS | ||
+ | } | ||
+ | |||
+ | $Program.Get() | ||
+ | $Program.SupportedOperatingSystems = $OS_Details | ||
+ | $Program.Put() | ||
+ | } | ||
+ | |||
+ | Function CreateProgram { | ||
+ | If(!(Get-Module ConfigurationManager)) {Import-Module "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager"} | ||
+ | cd TUY: | ||
+ | |||
+ | New-CMProgram ` | ||
+ | -PackageName $Args[0].Name ` | ||
+ | -StandardProgramName $Args[1].Name ` | ||
+ | -CommandLine $Args[1].CommandLine ` | ||
+ | -DiskSpaceRequirement $Args[1].DiskSpace ` | ||
+ | -DiskSpaceUnit $Args[1].DiskSpaceUnit ` | ||
+ | -ProgramRunType $Args[1].RunType ` | ||
+ | -Duration $Args[1].RunTime ` | ||
+ | -RunMode $Args[1].RunMode ` | ||
+ | -UserInterAction ([System.Convert]::ToBoolean($Args[1].UserInteraction)) *>$Null | ||
+ | |||
+ | $CMProgram = Get-CMProgram -PackageId $Args[0].PackageId -ProgramName $Args[1].Name | ||
+ | |||
+ | Switch ($Args[1].Bitness) | ||
+ | {"x64" {$CMProgram.ProgramFlags = $CMProgram.ProgramFlags -bxor ([math]::pow(2,27)); $CMProgram.Put(); SetOperatingSystemsx64 $CMProgram } | ||
+ | "x86" {$CMProgram.ProgramFlags = $CMProgram.ProgramFlags -bxor ([math]::pow(2,27)); $CMProgram.Put(); SetOperatingSystemsx86 $CMProgram } | ||
+ | } | ||
+ | |||
+ | |||
+ | If ($Args[1].RunAnotherProgram.Length -gt 0) {$CMProgram.DependentProgram = $Args[0].PackageId + ";;" +$Args[1].RunAnotherProgram;$CMProgram.Put();$CMProgram.ProgramFlags = $CMProgram.ProgramFlags -bxor ([math]::pow(2,7));$CMProgram.Put()} | ||
+ | |||
+ | #There is no argument for Comment in New-CMProgram so we have to add the Comment with Set-CMProgram. | ||
+ | |||
+ | Set-CMProgram ` | ||
+ | -StandardProgram ` | ||
+ | -InputObject $Args[0] ` | ||
+ | -StandardProgramName $Args[1].Name ` | ||
+ | -Comment $Args[1].Comment *>$Null | ||
+ | |||
+ | |||
+ | cd c: | ||
+ | } | ||
+ | |||
+ | #Main | ||
+ | If ($args.Length -eq 0) {Write-Host "No software given!";Exit} | ||
+ | |||
+ | #Read the package settings from PackageDetails.csv, {Name,Language,Manufacturer,Path,Version,DistributeContent}. All other are strings, DistributeContent is Boolean. | ||
+ | #Add Date to the name to randomize the package name. | ||
+ | $PackageDetails = Import-Csv $args[0] | ||
+ | $Date = Get-Date -Format d | ||
+ | $PackageName = $PackageDetails.Name + ".$Date" | ||
+ | $PaLang = $PackageDetails.Language | ||
+ | $PaManufacturer = $PackageDetails.Manufacturer | ||
+ | $PaPath = $PackageDetails.Path | ||
+ | $PaVersion = $PackageDetails.Version | ||
+ | |||
+ | #Load the module ConfigurationManager and change to the ConfigMgr site. | ||
+ | If(!(Get-Module ConfigurationManager)) {Import-Module "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager"} | ||
+ | cd TUY: | ||
+ | |||
+ | #Create the package. | ||
+ | New-CMPackage ` | ||
+ | -Name $PackageName ` | ||
+ | -Language $Palang ` | ||
+ | -Manufacturer $PaManufacturer ` | ||
+ | -Path $PaPath ` | ||
+ | -Version $PaVersion *>$Null | ||
+ | |||
+ | #Move the package to the right folder. | ||
+ | $NewCMPackage = Get-CMPackage -Name $PackageName | ||
+ | Move-CMObject -FolderPath "TUY:\Package\Test" -InputObject $NewCMPackage | ||
+ | |||
+ | #Distribute the content if needed. | ||
+ | If($PackageDetails.DistributeContent = "True") {Start-CMContentDistribution -Package $NewCMPackage -DistributionPointName configmgrdp.utu.fi} | ||
+ | |||
+ | cd c: | ||
+ | |||
+ | #Import programs from the Programs.csv, {Name,CommandLine,Comment,DiskSpace,DiskSpaceUnit,RunType,Runtime,UserInteraction,RunMode,Bitness,RunAnotherProgram} and create each program. | ||
+ | $Programs = Import-Csv $args[1] | ||
+ | ForEach ($Program in $Programs) {CreateProgram $NewCMPackage $Program} | ||
+ | |||
+ | cd TUY: | ||
+ | |||
+ | #Rename the package. | ||
+ | Set-CMPackage ` | ||
+ | -InputObject $NewCMPackage ` | ||
+ | -NewName $PackageDetails.Name *>$Null | ||
+ | |||
+ | cd c: | ||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
+ | FirefoxPackageDetails.csv: | ||
+ | |||
+ | Name,Language,Manufacturer,Path,Version,DistributeContent\\ | ||
+ | "Firefox","EN","Mozilla","\\utu.fi\verkkolevyt\Managed Applications\Mozilla Firefox\Mozilla Firefox 38.2.1 esr","38.2.2 ESR","True" | ||
+ | |||
+ | FirefoxPrograms.csv: | ||
+ | |||
+ | Name,CommandLine,Comment,DiskSpace,DiskSpaceUnit,RunType,Runtime,UserInteraction,RunMode,Bitness,RunAnotherProgram\\ | ||
+ | "Install, Uninstall (Machine)","wscript.exe runsoftware.vbs","Use this program to Install or Uninstall the machine part of the application.","80","1","0","30","True","1","x86",""\\ | ||
+ | "Install, Uninstall","wscript.exe profile.vbs","Use this program to Install or Uninstall the application.","80","1","0","30","True","0","x86","Install, Uninstall (Machine)" | ||
+ |