コード例 #1
0
        protected override void ProcessRecord()
        {
            string url = automationHelper.GetUrl();

            url = url + "/api/Script";

            WriteVerbose($"Using URL: {url}");
            //does it already exist?
            List <VMScript> result = automationHelper.GetWebCall <List <VMScript> >(url + $"?Name={Name}");
            bool            exists = result.Any(r => r.Name.Equals(Name));

            if (exists)
            {
                WriteVerbose($"Script already exists.");
                return;
            }

            //create new
            ScriptPostParameters postParams = new ScriptPostParameters();

            postParams.Description = Description;
            postParams.Name        = Name;
            string scriptID = automationHelper.PostWebCall(url, postParams);

            //create version
            if (!string.IsNullOrEmpty(Body))
            {
                url = automationHelper.GetUrl() + $"/api/Script/{scriptID}/Version";
                ScriptVersionPostParameters verPostParms = new ScriptVersionPostParameters();
                verPostParms.ScriptBody       = Body;
                verPostParms.ScriptLanguageId = 2; //this needs to be dynamic someday
                string versionID = automationHelper.PostWebCall(url, verPostParms);
                WriteVerbose($"Version ID: {versionID}");
            }

            //return script object
            url = automationHelper.GetUrl() + $"/api/Script/{scriptID}";
            WriteObject(automationHelper.GetWebCall <VMScript>(url));

            base.ProcessRecord();
        }
コード例 #2
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            if (ParameterSetName.Equals("File"))
            {
                if (File.Exists(ScriptFile))
                {
                    Body = File.ReadAllText(ScriptFile);
                }
            }
            Collection <PSParseError> parseErrors = new Collection <PSParseError>();

            PSParser.Tokenize(Body, out parseErrors);
            if (parseErrors.Count > 0)
            {
                var         exception = new Exception("Script parse error");
                ErrorRecord error     = new ErrorRecord(exception, "1", ErrorCategory.InvalidData, null);
                WriteError(error);
                return;
            }
            string url = automationHelper.GetUrl();

            url = url + "/api/Script";

            WriteVerbose($"Using URL: {url}");
            //does it already exist?
            List <VMScript> result   = automationHelper.GetWebCall <List <VMScript> >(url + $"?Name={Name}");
            bool            exists   = result.Any(r => r.Name.Equals(Name));
            string          scriptID = null;

            if (!exists)
            {
                //create new
                ScriptPostParameters postParams = new ScriptPostParameters();
                postParams.Description = Description;
                postParams.Name        = Name;
                scriptID = automationHelper.PostWebCall(url, postParams);
            }
            else
            {
                scriptID = result.Select(r => r.ScriptId).FirstOrDefault().ToString();
            }

            //create version
            if (!string.IsNullOrEmpty(Body) && scriptID != null)
            {
                url = automationHelper.GetUrl() + $"/api/Script/{scriptID}/Version";
                ScriptVersionPostParameters verPostParms = new ScriptVersionPostParameters();
                verPostParms.ScriptBody = Body;
                short lang = 0;
                switch (Language)
                {
                case "PowerShellCore":
                    lang = (short)ScriptLanguageEnum.PowerShellCore;
                    break;
                }
                verPostParms.ScriptLanguageId = lang;
                string versionID = automationHelper.PostWebCall(url, verPostParms);
            }

            //return script object
            url = automationHelper.GetUrl() + $"/api/Script/{scriptID}";
            WriteObject(automationHelper.GetWebCall <VMScript>(url));
        }