如何使用 SCCM 在 c# 中将文件替换为网络上的所有用户配置文件

How to replace a file to all user profiles on a network in c# using SCCM

我想从服务器复制一个 xml 文件,然后连接到所有用户配置文件并用从服务器复制的文件覆盖他们拥有的用户配置文件 xml 文件。我希望这是一个可执行文件,因此它可以 运行 与所有用户的 SCCM。我知道必须有管理权限,但我不确定如何对其进行编码。我也对如何以不同方式执行此操作的想法持开放态度,但我确实想使用 C# 执行此操作并将其制作成 SCCM 的可执行文件。

   namespace copy_delete_move_files
{
    public class SimpleFileCopy
    {
        public static object Logger { get; private set; }

        static void Main()
        {
            string fileName = "Customize.xml";
            string sourcePath = @"\pathToServer\c$\TestFolder";
            string targetPath = @"\pathToUserProfiles\c$\%USERPROFILE%\APPDATA\Roaming\Folder\Customize";

            // Use Path class to manipulate file and directory paths.
            string sourceFile = Path.Combine(sourcePath, fileName);       
            string destFile = Path.Combine(targetPath, fileName);

            // To copy a folder's contents to a new location:
            // Create a new target folder, if necessary.
            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }

            // To copy a file to another location and 
            // overwrite the destination file if it already exists.
            File.Copy(sourceFile, destFile, true);

            // To copy all the files in one directory to another directory.
            // Get the files in the source folder.
            // Note: Check for target path was performed previously
            // in this code example.

            if (Directory.Exists(sourcePath))
            {
                string[] files = Directory.GetFiles(sourcePath);

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = Path.GetFileName(s);
                    destFile = Path.Combine(targetPath, fileName);
                    File.Copy(s, destFile, true);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }

            // Keep console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

使用 SCCM,您可以使用以下步骤实现此目的:

  1. 以适用于一个用户的方式创建您的程序(整个多用户部分将留给 sccm,您只需以适用于任何用户的方式编写它,然后双击它).作为 appdata 路径,您只需使用类似的东西:

    string fileNameWithExt = "your folderpath and filename";
    string destPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fileNameWithExt);
    
  2. 在 SCCM 中,当您在向导中创建程序时,您 select "Program can run: Only when a user is logged on" 和 "Run mode: Run with user's rights"。创建后,您进入程序的属性,转到 "Advanced" 并选择 "When this program is assigned to a computer: Run once for every user that logs on"

这确保不仅现在所有用户而且将来所有用户都将获得该程序。如果您真的想从服务器复制,它也会帮助您,因为默认情况下 SCCM 使用本地系统帐户,该帐户没有访问您的服务器共享的权限(尽管您可以授予它们)。应用程序而不是程序也应该是可能的。

缺点是:

  • 您的所有用户都必须被允许访问该共享(可以是 通过使用 sccms 分发点方法绕过,但会更多 如果服务器上的文件经常更新会很复杂)
  • 如果您需要管理员权限才能进行任何操作,这是不可能的 方法。如果管理员权限仅适用于不是每个用户的部分 具体来说,您可以将程序分成两部分,每台机器一个, 每台电脑一个。