当前位置: 首页 > news >正文

[转]通过脚本添加登陆/注销/开机/关机脚本

由于最近需要使用到关机脚本,就研究了下怎样通过代码的方式添加关机脚步;下面是一篇参考文件,现在转发。

添加完脚步如果要马上生效需要刷新组策略,通过RefreshPolicy/RefreshPolicyEx/gpupdate 都可以

'------------------------------------------------------------------------------------------------------------
' This script is used to add Logon and Logoff scripts to a local Group Policy.
 
' It modifies/creates the following two files needed to run logon and logoff scripts:
' - "%SystemRoot%\system32\GroupPolicy\gpt.ini"
' - "%SystemRoot%\system32\GroupPolicy\User\Scripts\scripts.ini"
 
' Notes:
'        - This script will modify the existing gpt.ini file, but overwrites an existing scripts.ini file.
'          It does NOT append or modify the scripts.ini file.
'        - This script does not copy the actual logon/logoff scripts into place.
 
' The motivation to write this script was to overcome some challenges in a Novell environment due to a
' limitation of the integration with Novell Zen Policies, where logoff scripts are NOT supported on
' Terminal Servers.
 
' Release 1.1 Modified by Jeremy@jhouseconsulting.com on 27th October 2010.
' Written by Jeremy@jhouseconsulting.com on 25th August 2008.
 
' When managing the policy versions you should adhere to the Microsoft standards. The version numbering
' differs for changes made to the User and Computer configuration. In order to track changes to each
' configuration, the GPO must track a version number for each configuration. With only one version number,
' the way two versions are tracked is to split the version number into two numbers.
' The top 16 bits of the version number corresponds to the user configuration version. The lower 16 bits
' of the version number corresponds to the computer configuration version. When looking at the version
' entry in the gpt.ini file what you are then seeing is:
' Version = [user version number top 16 bits] [computer version number lower 16 bits]
' Esentially, each change to the User policy will increment the version by 131072, whilst each change to
' the Computer policy will increment the version by 1.
'
' http://blogs.technet.com/grouppolicy/archive/2007/12/14/understanding-the-gpo-version-number.aspx
'
' For example: In this script we are making a change to the User portion of the policy by adding a
' logon and logoff script. Therefore we will be incrementing the version number by 131072.
'
' We also need to add the Client Side Extension (CSE) GUIDs to instructs the winlogon process
' to run the Startup/Shutdown/Logon/Logoff scripts.
' CSE GUID for script processing = {42B5FAAE-6536-11D2-AE5A-0000F87571E3}
' Tool extension GUID for user and computer policy mode settings = {40B66650-4972-11D1-A7CA-0000F87571E3}
'
' gPCUserExtensionNames - includes a list of globally unique identifiers (GUIDs) that tells the client-side
' engine which client-side extensions have User data in the Group Policy object. The format is the following:
' [{<GUID of client-side extension>}{<GUID of MMC extension>}{<GUID of second MMC extension if appropriate>}]
'
' For example: In this script we are adding logon and logoff scripts. Therefore the gPCUserExtensionNames
' will be set to the following at minimum:
' gPCUserExtensionNames=[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B66650-4972-11D1-A7CA-0000F87571E3}]
'
' gPCMachineExtensionNames - includes a list of GUIDs that tells the client side engine which Client Side
' Extensions have Machine data in the GPO.
' The default GUIDs are as follows.
' gPCMachineExtensionNames=[{35378EAC-683F-11D2-A89A-00C04FBBCFA2}{0F6B957D-509E-11D1-A7CC-0000F87571E3}]
'
' gPCFunctionalityVersion - The Version number of the Group Policy extension tool that created the Group
' Policy object.
' The default version is 2.
'
' The Option setting has 4 values.
' 0 = Enable User and Computer Configurations
' 1 = Disable User Configuration ONLY
' 2 = Disable Computer Configuration ONLY
' 3 = Disable User and Computer Configurations
' If the Option value is not present, the User and Computer Configurations are both enabled.
'------------------------------------------------------------------------------------------------------------
 
Option Explicit
 
Dim WshShell, strSystemRoot, strGPOLocation, objFSO, objFile, blnOption, strLine, strContents
Dim arrLogonScripts, arrLogoffScripts, strValue, intUserPolicy, intComputerPolicy, blnUserPolicy
Dim blnComputerPolicy, strCSEUserGUID, strToolextensionUserGUID, blnSection, blnUserExtensionNames
Dim blnVersion, blnFunctionality, blnMachineExtensionNames, i
 
Const ForReading = 1
Const ForWriting = 2
 
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strSystemRoot = WshShell.ExpandEnvironmentStrings("%SystemRoot%")
strGPOLocation = strSystemRoot & "\system32\GroupPolicy\"
 
'********** These are the script variables that can be changed **********
 
' Setup the arrays for the location and names of the logon and logoff scripts.
' Note that if you don't want to use either one or the other, just leave the
' array blank, or comment the line out altogether.
arrLogonScripts = Array("%SystemRoot%\LoadQAT.cmd","%SystemRoot%\RestoreDesktopIconPositions.Cmd")
arrLogoffScripts = Array("%SystemRoot%\SaveQAT.cmd","%SystemRoot%\SaveDesktopIconPositions.Cmd")
 
'************************************************************************
 
strCSEUserGUID = "{42B5FAAE-6536-11D2-AE5A-0000F87571E3}"
strToolextensionUserGUID = "{40B66650-4972-11D1-A7CA-0000F87571E3}"
 
blnUserPolicy = True
blnComputerPolicy = False
 
intUserPolicy = 131072
intComputerPolicy = 1
 
blnSection = False
blnFunctionality = False
blnMachineExtensionNames = False
blnVersion = False
blnUserExtensionNames = False
blnOption = False
 
If objFSO.FileExists(strGPOLocation & "gpt.ini") Then
  Set objFile = objFSO.OpenTextFile(strGPOLocation & "gpt.ini", ForReading)
 
  Do While objFile.AtEndOfStream = False
    strLine = objFile.ReadLine
 
    If Instr(strLine,"[General]") > 0 Then
      blnSection = True
    End If
 
    If Len(strLine) > 0 AND Instr(strLine,";") <> 1 AND Instr(strLine,"[General]") <> 1 Then
    strValue=split(strLine,"=")
 
      Select Case strValue(0)
        Case "gPCFunctionalityVersion"
          blnFunctionality = True
        Case "gPCMachineExtensionNames"
          blnMachineExtensionNames = True
        Case "Version"
          If blnUserPolicy Then
            strValue(1)=strValue(1) + intUserPolicy
          End If
          blnVersion = True
        Case "gPCUserExtensionNames"
          If trim(strValue(1))="" Then
            strValue(1)="[" & strCSEUserGUID & strToolextensionUserGUID & "]"
            blnUserExtensionNames = True
          End If
          If instr(strValue(1),strCSEUserGUID) > 0 AND instr(strValue(1),strToolextensionUserGUID) > 0 Then
            blnUserExtensionNames = True
          Else
            If instr(strValue(1),strCSEUserGUID) = 0 Then
              If Right(trim(strValue(1)),1) = "]" Then
                strValue(1)=Left(trim(strValue(1)),Len(trim(strValue(1)))-1)
              End If
              strValue(1)=strValue(1) & strCSEUserGUID & "]"
              blnUserExtensionNames = True
            End If
            If instr(strValue(1),strToolextensionUserGUID) = 0 Then
              If Right(trim(strValue(1)),1) = "]" Then
                strValue(1)=Left(trim(strValue(1)),Len(trim(strValue(1)))-1)
              End If
              strValue(1)=strValue(1) & strToolextensionUserGUID & "]"
              blnUserExtensionNames = True
            End If
          End If
        Case "Options"
          If strValue(1)="0" Then
            blnOption = True
          End If
          If strValue(1)="1" Then
            strValue(1)="0"
            blnOption = True
          End If
          If strValue(1)="2" Then
            blnOption = True
          End If
          If strValue(1)="3" Then
            strValue(1)="2"
            blnOption = True
          End If
      End Select
      strContents = strContents & trim(strValue(0)) & "=" & trim(strValue(1)) & VbCrLf
    Else
      strContents = strContents & strLine & VbCrLf
    End If
  Loop
  objFile.Close
End If
 
If blnSection = False Then
  strContents="[General]" & VbCrLf & strContents
End If
If blnFunctionality = False Then
  strContents=strContents & "gPCFunctionalityVersion=2" & VbCrLf
End If
If blnMachineExtensionNames = False Then
  strContents=strContents & "gPCMachineExtensionNames=[{35378EAC-683F-11D2-A89A-00C04FBBCFA2}{0F6B957D-509E-11D1-A7CC-0000F87571E3}]" & VbCrLf
End If
If blnVersion = False Then
  If blnUserPolicy Then
    strContents=strContents & "Version=131073" & VbCrLf
  Else
    strContents=strContents & "Version=1" & VbCrLf
  End If
End If
If blnUserExtensionNames = False Then
  strContents=strContents & "gPCUserExtensionNames=[" & strCSEUserGUID & strToolextensionUserGUID & "]" & VbCrLf
End If
If blnOption = False Then
  strContents=strContents & "Options=0" & VbCrLf
End If
 
Set objFile = objFSO.OpenTextFile(strGPOLocation & "gpt.ini", ForWriting, True)
objFile.Write(strContents)
objFile.Close
Set objFile = Nothing
 
WScript.Echo "Local GPO Updated (gpt.ini): " & chr(34) & strGPOLocation & "gpt.ini" & chr(34)
 
If NOT objfso.FolderExists(strGPOLocation & "User") Then
  objfso.CreateFolder(strGPOLocation & "User")
End If
If NOT objfso.FolderExists(strGPOLocation & "User\Scripts") Then
  objfso.CreateFolder(strGPOLocation & "User\Scripts")
End If
If NOT objfso.FolderExists(strGPOLocation & "User\Scripts\Logoff") Then
  objfso.CreateFolder(strGPOLocation & "User\Scripts\Logoff")
End If
If NOT objfso.FolderExists(strGPOLocation & "User\Scripts\Logon") Then
  objfso.CreateFolder(strGPOLocation & "User\Scripts\Logon")
End If
 
' Need to unhide the scripts.ini file to prevent any "permission denied" errors.
If objfso.FileExists(strGPOLocation & "User\Scripts\scripts.ini") Then
  Set objFile = objFSO.GetFile(strGPOLocation & "User\Scripts\scripts.ini")
  If objFile.Attributes = objFile.Attributes AND 2 Then
    objFile.Attributes = objFile.Attributes XOR 2
  End If
  Set objFile = Nothing
End If
 
strContents = "[Logon]" & VbCrLf
If IsArray(arrLogonScripts) Then
  For i = 0 to ubound(arrLogonScripts)
    strContents = strContents & i & "CmdLine=" & arrLogonScripts(i) & VbCrLf
    strContents = strContents & i & "Parameters=" & VbCrLf
  Next
End If
strContents = strContents & "[Logoff]" & VbCrLf
If IsArray(arrLogoffScripts) Then
  For i = 0 to ubound(arrLogoffScripts)
    strContents = strContents & i & "CmdLine=" & arrLogoffScripts(i) & VbCrLf
    strContents = strContents & i & "Parameters=" & VbCrLf
  Next
End If
 
On Error Resume Next
Set objFile = objFSO.OpenTextFile(strGPOLocation & "User\Scripts\scripts.ini", ForWriting, True)
If Err.Number = 0 Then
  objFile.Write(strContents)
  objFile.Close
  Set objFile = Nothing
  WScript.Echo "Scripts Added (scripts.ini): " & chr(34) & strGPOLocation & "User\Scripts\scripts.ini" & chr(34)
' Hide the scripts.ini file.
  Set objFile = objFSO.GetFile(strGPOLocation & "User\Scripts\scripts.ini")
  objFile.Attributes = objFile.Attributes XOR 2
  Set objFile = Nothing
Else
  wscript.echo "Error: " & Err.Description & ". The scripts.ini file has not been set."
End If
On Error Goto 0
 
Set WshShell = Nothing
Set objFSO = Nothing
 
wscript.quit (0)

 

转载于:https://www.cnblogs.com/Quincy/p/5018383.html

相关文章:

  • 学习 AngularJS (三) module
  • Json序列化之.NET开源类库Newtonsoft.Json
  • C/C++ 库函数 是否调用 WinAPI
  • 构造汽车
  • 违反约束或者主外键删除数据
  • 不平衡学习方法理论和实战总结
  • Windows Cygwin Redis 安装(转)
  • apche commons项目简介
  • CAS (2) —— Mac下配置CAS到Tomcat(客户端)
  • Median of Two Sorted Arrays
  • 固态硬盘上安装Windows8(ghost)启动问题
  • 王家林每日大数据语录Spark篇0003
  • [hive小技巧]同一份数据多种处理
  • 【191】◀▶ Powershell 命令集 Cmdlets
  • Github访问慢解决办法
  • @jsonView过滤属性
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • 10个确保微服务与容器安全的最佳实践
  • 2017 前端面试准备 - 收藏集 - 掘金
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • es6(二):字符串的扩展
  • iOS | NSProxy
  • Java,console输出实时的转向GUI textbox
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • tab.js分享及浏览器兼容性问题汇总
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 分布式任务队列Celery
  • 基于webpack 的 vue 多页架构
  • 记录一下第一次使用npm
  • 删除表内多余的重复数据
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 用Canvas画一棵二叉树
  • 转载:[译] 内容加速黑科技趣谈
  • 带你开发类似Pokemon Go的AR游戏
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • #Java第九次作业--输入输出流和文件操作
  • (二)JAVA使用POI操作excel
  • (三十五)大数据实战——Superset可视化平台搭建
  • (一) storm的集群安装与配置
  • (转)shell中括号的特殊用法 linux if多条件判断
  • .NET Micro Framework 4.2 beta 源码探析
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)
  • .netcore 获取appsettings
  • .Net面试题4
  • @Autowired多个相同类型bean装配问题
  • @GlobalLock注解作用与原理解析
  • @Pointcut 使用
  • @RestController注解的使用
  • [ vulhub漏洞复现篇 ] Grafana任意文件读取漏洞CVE-2021-43798
  • [16/N]论得趣
  • [Codeforces] number theory (R1600) Part.11
  • [dart学习]第四篇:函数
  • [Erlang 0129] Erlang 杂记 VI 2014年10月28日