diff --git a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Commands.ResourceManager.Cmdlets.csproj b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Commands.ResourceManager.Cmdlets.csproj
index e76c7007c983..9a47aa2b11b4 100644
--- a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Commands.ResourceManager.Cmdlets.csproj
+++ b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Commands.ResourceManager.Cmdlets.csproj
@@ -143,6 +143,8 @@
+
+
diff --git a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/TestAzureResourceCmdlet.cs b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/TestAzureResourceCmdlet.cs
new file mode 100644
index 000000000000..5d38636d9610
--- /dev/null
+++ b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/TestAzureResourceCmdlet.cs
@@ -0,0 +1,188 @@
+// ----------------------------------------------------------------------------------
+//
+// Copyright Microsoft Corporation
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// ----------------------------------------------------------------------------------
+
+namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
+{
+ using System;
+ using System.Management.Automation;
+ using System.Net;
+ using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
+ using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
+ using Newtonsoft.Json.Linq;
+
+ ///
+ /// Cmdlet to check if a resource exists or not
+ ///
+ [Cmdlet(VerbsDiagnostic.Test, "AzureResource", DefaultParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet), OutputType(typeof(bool))]
+ public sealed class TestAzureResoruceCmdlet : ResourceManagerCmdletBase
+ {
+ ///
+ /// The get resource parameter set.
+ ///
+ internal const string GetResourceGroupResourceParameterSet = "Tests for the existance of a single resource in a resource group.";
+
+ ///
+ /// The get tenant resource parameter set.
+ ///
+ internal const string GetTenantResourceParameterSet = "Tests for the existance of a single resource at the tenant level.";
+
+ ///
+ /// The get tenant resource parameter set.
+ ///
+ internal const string GetResourceByIdParameterSet = "Tests for the existance of a single resource by its Id.";
+
+ ///
+ /// The get tenant resource parameter set.
+ ///
+ internal const string GetSubscriptionResourcesParameterSet = "Tests for the existance of a single resource at the subscription level.";
+
+ ///
+ /// Gets or sets the resource name parameter.
+ ///
+ [Alias("Id")]
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetResourceByIdParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource's Id.")]
+ [ValidateNotNullOrEmpty]
+ public string ResourceId { get; set; }
+
+ ///
+ /// Gets or sets the resource name parameter.
+ ///
+ [Alias("Name")]
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
+ [ValidateNotNullOrEmpty]
+ public string ResourceName { get; set; }
+
+ ///
+ /// Gets or sets the resource type parameter.
+ ///
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
+ [ValidateNotNullOrEmpty]
+ public string ResourceType { get; set; }
+
+ ///
+ /// Gets or sets the extension resource name parameter.
+ ///
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource name. e.g. to specify a database MyServer/MyDatabase.")]
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource name. e.g. to specify a database MyServer/MyDatabase.")]
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource type. e.g. Microsoft.Sql/Servers/Databases.")]
+ [ValidateNotNullOrEmpty]
+ public string ExtensionResourceName { get; set; }
+
+ ///
+ /// Gets or sets the extension resource type.
+ ///
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource type. e.g. Microsoft.Sql/Servers/Databases.")]
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource type. e.g. Microsoft.Sql/Servers/Databases.")]
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource type. e.g. Microsoft.Sql/Servers/Databases.")]
+ [ValidateNotNullOrEmpty]
+ public string ExtensionResourceType { get; set; }
+ ///
+ /// Gets or sets the subscription ids.
+ ///
+ [Parameter(Mandatory = false, ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
+ [Parameter(Mandatory = false, ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
+ [ValidateNotNullOrEmpty]
+ public Guid? SubscriptionId { get; set; }
+
+ ///
+ /// Gets or sets the resource group name.
+ ///
+ [Parameter(Mandatory = true, ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")]
+ [ValidateNotNullOrEmpty]
+ public string ResourceGroupName { get; set; }
+
+ ///
+ /// Gets or sets the tenant level parameter.
+ ///
+ [Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet, Mandatory = true, HelpMessage = "Indicates that this is a tenant level operation.")]
+ public SwitchParameter TenantLevel { get; set; }
+
+ ///
+ /// Collects subscription ids from the pipeline.
+ ///
+ protected override void OnProcessRecord()
+ {
+ base.OnProcessRecord();
+ if (!this.TenantLevel)
+ {
+ this.SubscriptionId = this.Profile.Context.Subscription.Id;
+ }
+
+ this.RunCmdlet();
+ }
+
+ ///
+ /// Contains the cmdlet's execution logic.
+ ///
+ private void RunCmdlet()
+ {
+ this.WriteObject(this.TestResource());
+ }
+
+ ///
+ /// Tests if a resource exists or not.
+ ///
+ private bool TestResource()
+ {
+ var resourceId = this.GetResourceId();
+
+ var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;
+
+ try
+ {
+ this.GetResourcesClient().GetResource(resourceId: resourceId, apiVersion: apiVersion, cancellationToken: this.CancellationToken.Value).Wait();
+ return true;
+ }
+ catch (Exception ex)
+ {
+ if(ex.InnerException != null && ex.InnerException is ErrorResponseMessageException)
+ {
+ var exception = ex.InnerException as ErrorResponseMessageException;
+ if (exception.HttpStatus.Equals(HttpStatusCode.NotFound))
+ {
+ return false;
+ }
+ else
+ {
+ throw ex.InnerException;
+ }
+ }
+ else
+ {
+ throw ex;
+ }
+ }
+ }
+
+ ///
+ /// Gets the resource Id from the supplied PowerShell parameters.
+ ///
+ private string GetResourceId()
+ {
+ return !string.IsNullOrWhiteSpace(this.ResourceId)
+ ? this.ResourceId
+ : ResourceIdUtility.GetResourceId(
+ subscriptionId: this.SubscriptionId,
+ resourceGroupName: this.ResourceGroupName,
+ resourceType: this.ResourceType,
+ resourceName: this.ResourceName,
+ extensionResourceType: this.ExtensionResourceType,
+ extensionResourceName: this.ExtensionResourceName);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/TestAzureResourceGroupCmdlet.cs b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/TestAzureResourceGroupCmdlet.cs
new file mode 100644
index 000000000000..3de440f13e6f
--- /dev/null
+++ b/src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Implementation/TestAzureResourceGroupCmdlet.cs
@@ -0,0 +1,108 @@
+// ----------------------------------------------------------------------------------
+//
+// Copyright Microsoft Corporation
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// ----------------------------------------------------------------------------------
+
+namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
+{
+ using System;
+ using System.Management.Automation;
+ using System.Net;
+ using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
+ using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
+ using Newtonsoft.Json.Linq;
+
+ ///
+ /// Cmdlet to check if a resource group exists or not
+ ///
+ [Cmdlet(VerbsDiagnostic.Test, "AzureResourceGroup"), OutputType(typeof(bool))]
+ public sealed class TestAzureResoruceGroupCmdlet : ResourceManagerCmdletBase
+ {
+ ///
+ /// Gets or sets the subscription ids.
+ ///
+ [Parameter(Mandatory = false, ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
+ [Parameter(Mandatory = false, ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
+ [ValidateNotNullOrEmpty]
+ public Guid? SubscriptionId { get; set; }
+
+ ///
+ /// Gets or sets the resource group name.
+ ///
+ [Parameter(Mandatory = true, ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")]
+ [ValidateNotNullOrEmpty]
+ public string ResourceGroupName { get; set; }
+
+ ///
+ /// Collects subscription ids from the pipeline.
+ ///
+ protected override void OnProcessRecord()
+ {
+ base.OnProcessRecord();
+ this.SubscriptionId = this.Profile.Context.Subscription.Id;
+ this.RunCmdlet();
+ }
+
+ ///
+ /// Contains the cmdlet's execution logic.
+ ///
+ private void RunCmdlet()
+ {
+ this.WriteObject(this.TestResourceGroup());
+ }
+
+ ///
+ /// Tests if a resource group exists or not.
+ ///
+ private bool TestResourceGroup()
+ {
+ var resourceGroupId = this.GetResourceGroupId();
+ var apiVersion = this.DetermineApiVersion(resourceId: resourceGroupId).Result;
+ try
+ {
+ this.GetResourcesClient().GetResource(resourceId: resourceGroupId, apiVersion: apiVersion, cancellationToken: this.CancellationToken.Value).Wait();
+ return true;
+ }
+ catch (Exception ex)
+ {
+ if (ex.InnerException != null && ex.InnerException is ErrorResponseMessageException)
+ {
+ var exception = ex.InnerException as ErrorResponseMessageException;
+ if (exception.HttpStatus.Equals(HttpStatusCode.NotFound))
+ {
+ return false;
+ }
+ else
+ {
+ throw ex.InnerException;
+ }
+ }
+ else
+ {
+ throw ex;
+ }
+ }
+ }
+
+ ///
+ /// Gets the resource Id from the supplied PowerShell parameters.
+ ///
+ private string GetResourceGroupId()
+ {
+ return ResourceIdUtility.GetResourceId(
+ subscriptionId: this.SubscriptionId,
+ resourceGroupName: this.ResourceGroupName,
+ resourceType: null,
+ resourceName: null);
+ }
+ }
+}
\ No newline at end of file