Guest User

Untitled

a guest
Jan 20th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.29 KB | None | 0 0
  1. //
  2. // File History Sample Setup Tool
  3. // Copyright (c) Microsoft Corporation.  All Rights Reserved.
  4. //
  5.  
  6. #include <fhsetup.h>
  7.  
  8. HRESULT ScheduleBackups()
  9. /*++
  10.  
  11. Routine Description:
  12.  
  13.     This function starts the File History service if it is stopped
  14.     and schedules regular backups.
  15.  
  16. Arguments:
  17.  
  18.     None
  19.  
  20. Return Value:
  21.  
  22.     S_OK if successful
  23.     HRESULT from underlying functions
  24.  
  25. --*/
  26. {
  27.     HRESULT backupHr = S_OK;
  28.     HRESULT pipeHr = S_OK;
  29.     FH_SERVICE_PIPE_HANDLE pipe = NULL;
  30.  
  31.     pipeHr = FhServiceOpenPipe(TRUE, &pipe);
  32.     if (SUCCEEDED(pipeHr))
  33.     {
  34.         backupHr = FhServiceReloadConfiguration(pipe);
  35.         pipeHr = FhServiceClosePipe(pipe);
  36.     }
  37.  
  38.     // The HRESULT from the backup operation is more important than
  39.     // the HRESULT from pipe operations
  40.     return FAILED(backupHr) ? backupHr : pipeHr;
  41. }
  42.  
  43. HRESULT ConfigureFileHistory(
  44.     _In_ PWSTR TargetPath
  45.     )
  46. /*++
  47.  
  48. Routine Description:
  49.  
  50.     This function configures a target for File History.
  51.     It will only succeed if the user has never configured File History
  52.     before and there is no File History data on the target.
  53.  
  54. Arguments:
  55.  
  56.     TargetPath -
  57.         The path of the File History target
  58.  
  59. Return Value:
  60.  
  61.     S_OK if successful
  62.     E_INVALIDARG if TargetPath is NULL
  63.     E_FAIL if configuration failed because File History is disabled by
  64.            group policy or the target is not valid
  65.     HRESULT from underlying functions
  66.  
  67. --*/
  68. {
  69.     HRESULT hr = S_OK;
  70.     FH_BACKUP_STATUS backupStatus;
  71.     FH_DEVICE_VALIDATION_RESULT validationResult;
  72.     CComPtr<IFhConfigMgr> configMgr;
  73.     CComBSTR targetPath;
  74.     CComBSTR targetName;
  75.  
  76.     // TargetPath must not be NULL
  77.     if (TargetPath == NULL)
  78.     {
  79.         hr = E_INVALIDARG;
  80.         goto Cleanup;
  81.     }
  82.      
  83.     // Copy the target path into a local variable and set the target name
  84.     // to an empty string to allow the config manager to set a name
  85.     _ATLTRY
  86.     {
  87.         targetPath = TargetPath;
  88.         targetName = L"";
  89.     }
  90.     _ATLCATCH(e)
  91.     {
  92.         hr = e;
  93.         goto Cleanup;
  94.     }
  95.  
  96.     // The configuration manager is used to create and load configuration  
  97.     // files, get/set the backup status, validate a target, etc
  98.     hr = configMgr.CoCreateInstance(CLSID_FhConfigMgr);
  99.     if (FAILED(hr))
  100.     {
  101.         wprintf(L"Error: CoCreateInstance failed (0x%X)\n", hr);
  102.         goto Cleanup;
  103.     }
  104.      
  105.     // Create a new default configuration file - do not overwrite if one
  106.     // already exists
  107.     wprintf(L"Creating default configuration\n");
  108.     hr = configMgr->CreateDefaultConfiguration(FALSE);
  109.     if (FAILED(hr))
  110.     {
  111.         if (hr == FHCFG_E_CONFIG_ALREADY_EXISTS)
  112.         {
  113.             wprintf(L"Error: File History has previously been configured\n");
  114.         }
  115.         else
  116.         {
  117.             wprintf(L"Error: CreateDefaultConfiguration failed (0x%X)\n", hr);
  118.         }
  119.         goto Cleanup;
  120.     }
  121.  
  122.     // Check the backup status
  123.     // If File History is disabled by group policy, quit
  124.     wprintf(L"Getting backup status\n");
  125.     hr = configMgr->GetBackupStatus(&backupStatus);
  126.     if (FAILED(hr))
  127.     {
  128.         wprintf(L"Error: GetBackupStatus failed (0x%X)\n", hr);
  129.         goto Cleanup;
  130.     }
  131.     if (backupStatus == FH_STATUS_DISABLED_BY_GP)
  132.     {
  133.         wprintf(L"Error: File History is disabled by group policy\n");
  134.         hr = E_FAIL;
  135.         goto Cleanup;
  136.     }
  137.  
  138.     // Make sure the target is valid to be used for File History
  139.     wprintf(L"Validating target\n");
  140.     hr = configMgr->ValidateTarget(targetPath, &validationResult);
  141.     if (FAILED(hr))
  142.     {
  143.         wprintf(L"Error: ValidateTarget failed (0x%X)\n", hr);
  144.         goto Cleanup;
  145.     }
  146.     if (validationResult != FH_VALID_TARGET)
  147.     {
  148.         // If the target is inaccessible, read-only, an invalid drive type
  149.         // (such as a CD), already being used for File History, or part of
  150.         // the protected namespace - don't enable File History
  151.         wprintf(L"Error: %ws is not a valid target\n", targetPath.m_str);
  152.         hr = E_FAIL;
  153.         goto Cleanup;
  154.     }
  155.  
  156.     // Provision the target to be used for File History and set
  157.     // it as the default target
  158.     wprintf(L"Provisioning and setting target\n");
  159.     hr = configMgr->ProvisionAndSetNewTarget(targetPath, targetName);
  160.     if (FAILED(hr))
  161.     {
  162.         wprintf(L"Error: ProvisionAndSetNewTarget failed (0x%X)\n", hr);
  163.         goto Cleanup;
  164.     }
  165.      
  166.     // Enable File History
  167.     wprintf(L"Enabling File History\n");
  168.     hr = configMgr->SetBackupStatus(FH_STATUS_ENABLED);
  169.     if (FAILED(hr))
  170.     {
  171.         wprintf(L"Error: SetBackupStatus failed (0x%X)\n", hr);
  172.         goto Cleanup;
  173.     }
  174.  
  175.     // Save the configuration to disk
  176.     wprintf(L"Saving configuration\n");
  177.     hr = configMgr->SaveConfiguration();
  178.     if (FAILED(hr))
  179.     {
  180.         wprintf(L"Error: SaveConfiguration failed (0x%X)\n", hr);
  181.         goto Cleanup;
  182.     }
  183.  
  184.     // Tell the File History service to schedule backups
  185.     wprintf(L"Scheduling regular backups\n");
  186.     hr = ScheduleBackups();
  187.     if (FAILED(hr))
  188.     {
  189.         wprintf(L"Error: ScheduleBackups failed (0x%X)\n", hr);
  190.         goto Cleanup;
  191.     }
  192.  
  193.     // Recommend the target to other Homegroup members
  194.     wprintf(L"Recommending target to Homegroup\n");
  195.     HRESULT hrRecommend = configMgr->ChangeDefaultTargetRecommendation(TRUE);
  196.     if (FAILED(hrRecommend))
  197.     {
  198.         wprintf(L"Warning: Failed to recommend target to Homegroup (0x%X)\n", hrRecommend);
  199.     }
  200.  
  201.     wprintf(L"Success! File History is now enabled\n");
  202.  
  203. Cleanup:
  204.     return hr;
  205. }
  206.  
  207. int __cdecl wmain(
  208.     _In_ int Argc,
  209.     _In_reads_(Argc) PWSTR Argv[]
  210.     )
  211. /*++
  212.  
  213. Routine Description:
  214.  
  215.     This is the main entry point of the console application.
  216.  
  217. Arguments:
  218.  
  219.     Argc - the number of command line arguments
  220.     Argv - command line arguments
  221.  
  222. Return Value:
  223.  
  224.     exit code
  225.  
  226. --*/
  227. {
  228.     HRESULT hr = S_OK;
  229.     BOOL comInitialized = FALSE;
  230.  
  231.     wprintf(L"\nFile History Sample Setup Tool\n");
  232.     wprintf(L"Copyright (C) Microsoft Corporation. All rights reserved.\n\n");
  233.  
  234.     // If there are fewer than 2 command-line arguments, print the correct
  235.     // usage and exit
  236.     if (Argc < 2)
  237.     {
  238.         wprintf(L"Usage: fhsetup <path>\n\n");
  239.         wprintf(L"Examples:\n");
  240.         wprintf(L"    fhsetup D:\\\n");
  241.         wprintf(L"    fhsetup \\\\server\\share\\\n\n");
  242.         goto Cleanup;
  243.     }
  244.  
  245.     // COM is needed to use the Config Manager
  246.     wprintf(L"Initializing COM...\n");
  247.     hr = CoInitialize(NULL);
  248.     if (FAILED(hr))
  249.     {
  250.         wprintf(L"Error: CoInitialize failed (0x%X)\n", hr);
  251.         goto Cleanup;
  252.     }
  253.     comInitialized = TRUE;
  254.  
  255.     hr = ConfigureFileHistory(Argv[1]);
  256.     if (FAILED(hr))
  257.     {
  258.         wprintf(L"File History configuration failed (0x%X)\n", hr);
  259.         goto Cleanup;
  260.     }
  261.  
  262. Cleanup:
  263.     // If COM was initialized, make sure it is uninitialized
  264.     if (comInitialized)
  265.     {
  266.         CoUninitialize();
  267.         comInitialized = FALSE;
  268.     }
  269.  
  270.     return 0;
  271. }
Add Comment
Please, Sign In to add comment