找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 23083|回复: 6

[文件操作] 提权删除用户文件时,如何获取当前域用户名称

[复制链接]
发表于 2018-3-8 11:43:50 | 显示全部楼层 |阅读模式
本帖最后由 pcl_test 于 2018-3-9 20:56 编辑

提权删除用户文件时,遇到的一个无奈的问题
具体描述一下:
1、C:\user文件夹下有诸多文件,我只需要保留当前域用户文件夹(%username%)、本地管理员(adminad)
2、需要用户环境执行批处理,所以想到了提权。
3、runas提权过后,%username%的值变成了adminad,而不是用户的域账户名称了。所以会出错。

新菜求解释,如何处理。
  1. @echo off
  2. ::for /d %%i in (C:\Users\*) do if /i "%%~ni" neq "%username%" if /i "%%~ni" neq "adminad"  rd /s /q "%%i"
  3. pause
复制代码
发表于 2018-3-8 17:52:11 | 显示全部楼层
for 循环里面要开启变量延迟
发表于 2018-3-8 18:05:58 | 显示全部楼层
本帖最后由 ivor 于 2018-3-8 20:46 编辑

1.following registry key should be remove:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList


2.Use My Computer or Windows Explorer to remove the appropriate Windows\Profiles\User name folders. To remove all profiles, remove the Windows\Profiles folder.


所以说注册表你也要操作,否则会出先下次登陆无法准备桌面
 楼主| 发表于 2018-3-8 18:12:09 | 显示全部楼层
回复 3# ivor
对,注册表得同步删除,否则就会临时文件登录或者无法加载配置文件。
我要通过runas调用,for开启变量延迟可以实现么?
发表于 2018-3-8 19:10:08 | 显示全部楼层
你可以通过传递参数%1 获取标准用户名
发表于 2018-3-8 22:06:16 | 显示全部楼层
本帖最后由 ivor 于 2018-3-9 09:30 编辑
  1. using System;
  2. using System.Diagnostics;
  3. using System.Security;
  4. using System.IO;
  5. using Microsoft.Win32;

  6. /**
  7.     author:ivor
  8.     提权调用自己然后按条件删除C:\\Users子文件夹 和 ProfileList 子项
  9.     */


  10. namespace UpPrivilege
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {

  16.             if (args.Length == 0)
  17.             {
  18.                 Lanch(Environment.UserName, Environment.CommandLine);
  19.             }
  20.             else
  21.             {
  22.                 DeleteUserProfile(args[0]);
  23.                 Console.Write("press any key to exit....");
  24.                 Console.ReadKey();
  25.             }

  26.         }
  27.         public static void Lanch(String domainUser, String file)
  28.         {
  29.             /**
  30.                 提权运行
  31.                 测试域local.domain用户test密码test
  32.             */
  33.             ProcessStartInfo psi = new ProcessStartInfo();
  34.             psi.Domain = "local.domain";
  35.             psi.UserName = "test";
  36.             psi.Password = ConvertToSecuretString("test");
  37.             psi.FileName = file;
  38.             psi.Arguments = domainUser;
  39.             psi.UseShellExecute = false;

  40.             Process p = new Process();
  41.             p.StartInfo = psi;
  42.             p.Start();

  43.         }


  44.         public static SecureString ConvertToSecuretString(String passWord)
  45.         {
  46.             /**
  47.                 加密密码字符串
  48.             */
  49.             if (passWord == null)
  50.                 throw new ArgumentNullException("passWord");
  51.             unsafe
  52.             {
  53.                 fixed (char* passwordChars = passWord)
  54.                 {
  55.                     SecureString securePassword = new SecureString(passwordChars, passWord.Length);
  56.                     securePassword.MakeReadOnly();
  57.                     return securePassword;
  58.                 }

  59.             }

  60.         }

  61.         public static void DeleteUserProfile(String domainUser)
  62.         {
  63.             /**
  64.                 删除用户文件夹
  65.             */
  66.             const String REG_PATH = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
  67.             const String USERS_FOLDER = "C:\\Users";
  68.             DirectoryInfo dir = new DirectoryInfo(USERS_FOLDER);
  69.             DirectoryInfo[] dirs = dir.GetDirectories();
  70.             foreach (DirectoryInfo d in dirs)
  71.             {
  72.                 if (d.Name != domainUser & d.Name != "adminad")
  73.                 {
  74.                     Console.WriteLine("{0} is deleting!", d.Name);
  75.                     Console.WriteLine(d.FullName);
  76.                     Console.ReadKey();
  77.                     d.Delete(true);
  78.                     DeleteProfileList(REG_PATH, d.FullName);

  79.                 }

  80.             }



  81.         }
  82.         public static bool DeleteProfileList(String profileList, String domainUser)
  83.         {
  84.             /**
  85.                 清理注册表
  86.             */
  87.             String subkey;
  88.             RegistryKey subReg;
  89.             RegistryKey key = Registry.LocalMachine;
  90.             RegistryKey myreg = key.OpenSubKey(profileList, true);
  91.             Console.WriteLine(profileList);

  92.             foreach (var k in myreg.GetSubKeyNames())
  93.             {
  94.                 Console.WriteLine("=========items=========");
  95.                 Console.WriteLine(k);
  96.                 Console.WriteLine("=========items=========");
  97.                 subkey = String.Format("{0}\\{1}", profileList, k);
  98.                 subReg = key.OpenSubKey(subkey);
  99.                 foreach (var sk in subReg.GetValueNames())
  100.                 {
  101.                     if (sk == "ProfileImagePath")
  102.                     {
  103.                         String ProfileImagePath = subReg.GetValue(sk).ToString();
  104.                         if (ProfileImagePath == domainUser)
  105.                         {
  106.                             Console.WriteLine(ProfileImagePath + "\n");
  107.                             myreg.DeleteSubKeyTree(k);
  108.                             return true;
  109.                         }
  110.                     }

  111.                 }
  112.             }
  113.             return false;
  114.         }


  115.     }
  116. }
复制代码
 楼主| 发表于 2018-3-9 09:50:34 | 显示全部楼层
回复 6# ivor 老铁你简直6的飞起。批处理估计实现这个功能比较费劲。C/C++也是个不错的选择。谢谢老铁。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 02:00 , Processed in 0.018952 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表