Add-Content : Access to the path 'C:' is denied












0















Hello i have this problem



My problem:
PS C:WINDOWSsystem32> escribirInfo
Add-Content : Access to the path 'C:' is denied.
At C:Untitled2.ps1:41 char:72
+ ... SerialNumber, Version, Manufacturer | Add-Content C:$nombre_fichero
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C::String) [Add-Content], UnauthorizedAccessException
+ FullyQualifiedErrorId : GetContentWriterUnauthorizedAccessError,Microsoft.PowerShell.Commands.AddContentCommand



main{
crearFichero
escribirInfo
}
function crearFichero
{
#Expliqueu que fa la funció!!!
[CmdletBinding()]
Param(

)
Begin
{
#RES
}
Process
{
$nombre_sistema = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name
$ip = Test-Connection $nombre_sistema -Count 1 | select IPV4Address
$ip_replace = $ip.IPV4Address.IPAddressToString.Replace(".","_")
$fecha = Get-Date -Format "MM_dd_yyyy"
$ext = ".txt"
$nombre_fichero = [System.String]::Concat($ip_replace, $fecha, $ext)
$crear_fichero= New-Item C:$nombre_fichero -ItemType file
}
}

function escribirInfo
{
#Expliqueu que fa la funció!!!
[CmdletBinding()]
Param(

)
Begin
{
#RES
}
Process
{
gwmi win32_bios | select SerialNumber, Version, Manufacturer | Add-Content C:$nombre_fichero

}
}
}









share|improve this question

























  • [1] why all the powershell version tags? please, only use the one that fits your setup. [2] please show the code that triggered the error.

    – Lee_Dailey
    Jan 18 at 21:54













  • Okeey i have already done it

    – Alberto Martín Rodríguez
    Jan 18 at 22:00











  • you have several problems in your code --- [1] the main {} structure is NOT used in powershell [2] your functions need to be defined BEFORE they are called. you have that reversed. [3] variables defined in a function are local to that function. your line 41 is attempting to write to C: - not to the file in the root ... to the root itself. WHY? because $nombre_fichero does not exist in the 2nd function - it was defined in the 1st function.

    – Lee_Dailey
    Jan 18 at 22:07













  • I'm a beginner i do not know hoy to do it

    – Alberto Martín Rodríguez
    Jan 18 at 22:24











  • start with just one thing - one very simple thing. make it work. add another thing. make them work with each other. don't use function util you have the items working with each other ... and even then, use functions only if they are needed.

    – Lee_Dailey
    Jan 18 at 22:33
















0















Hello i have this problem



My problem:
PS C:WINDOWSsystem32> escribirInfo
Add-Content : Access to the path 'C:' is denied.
At C:Untitled2.ps1:41 char:72
+ ... SerialNumber, Version, Manufacturer | Add-Content C:$nombre_fichero
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C::String) [Add-Content], UnauthorizedAccessException
+ FullyQualifiedErrorId : GetContentWriterUnauthorizedAccessError,Microsoft.PowerShell.Commands.AddContentCommand



main{
crearFichero
escribirInfo
}
function crearFichero
{
#Expliqueu que fa la funció!!!
[CmdletBinding()]
Param(

)
Begin
{
#RES
}
Process
{
$nombre_sistema = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name
$ip = Test-Connection $nombre_sistema -Count 1 | select IPV4Address
$ip_replace = $ip.IPV4Address.IPAddressToString.Replace(".","_")
$fecha = Get-Date -Format "MM_dd_yyyy"
$ext = ".txt"
$nombre_fichero = [System.String]::Concat($ip_replace, $fecha, $ext)
$crear_fichero= New-Item C:$nombre_fichero -ItemType file
}
}

function escribirInfo
{
#Expliqueu que fa la funció!!!
[CmdletBinding()]
Param(

)
Begin
{
#RES
}
Process
{
gwmi win32_bios | select SerialNumber, Version, Manufacturer | Add-Content C:$nombre_fichero

}
}
}









share|improve this question

























  • [1] why all the powershell version tags? please, only use the one that fits your setup. [2] please show the code that triggered the error.

    – Lee_Dailey
    Jan 18 at 21:54













  • Okeey i have already done it

    – Alberto Martín Rodríguez
    Jan 18 at 22:00











  • you have several problems in your code --- [1] the main {} structure is NOT used in powershell [2] your functions need to be defined BEFORE they are called. you have that reversed. [3] variables defined in a function are local to that function. your line 41 is attempting to write to C: - not to the file in the root ... to the root itself. WHY? because $nombre_fichero does not exist in the 2nd function - it was defined in the 1st function.

    – Lee_Dailey
    Jan 18 at 22:07













  • I'm a beginner i do not know hoy to do it

    – Alberto Martín Rodríguez
    Jan 18 at 22:24











  • start with just one thing - one very simple thing. make it work. add another thing. make them work with each other. don't use function util you have the items working with each other ... and even then, use functions only if they are needed.

    – Lee_Dailey
    Jan 18 at 22:33














0












0








0








Hello i have this problem



My problem:
PS C:WINDOWSsystem32> escribirInfo
Add-Content : Access to the path 'C:' is denied.
At C:Untitled2.ps1:41 char:72
+ ... SerialNumber, Version, Manufacturer | Add-Content C:$nombre_fichero
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C::String) [Add-Content], UnauthorizedAccessException
+ FullyQualifiedErrorId : GetContentWriterUnauthorizedAccessError,Microsoft.PowerShell.Commands.AddContentCommand



main{
crearFichero
escribirInfo
}
function crearFichero
{
#Expliqueu que fa la funció!!!
[CmdletBinding()]
Param(

)
Begin
{
#RES
}
Process
{
$nombre_sistema = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name
$ip = Test-Connection $nombre_sistema -Count 1 | select IPV4Address
$ip_replace = $ip.IPV4Address.IPAddressToString.Replace(".","_")
$fecha = Get-Date -Format "MM_dd_yyyy"
$ext = ".txt"
$nombre_fichero = [System.String]::Concat($ip_replace, $fecha, $ext)
$crear_fichero= New-Item C:$nombre_fichero -ItemType file
}
}

function escribirInfo
{
#Expliqueu que fa la funció!!!
[CmdletBinding()]
Param(

)
Begin
{
#RES
}
Process
{
gwmi win32_bios | select SerialNumber, Version, Manufacturer | Add-Content C:$nombre_fichero

}
}
}









share|improve this question
















Hello i have this problem



My problem:
PS C:WINDOWSsystem32> escribirInfo
Add-Content : Access to the path 'C:' is denied.
At C:Untitled2.ps1:41 char:72
+ ... SerialNumber, Version, Manufacturer | Add-Content C:$nombre_fichero
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C::String) [Add-Content], UnauthorizedAccessException
+ FullyQualifiedErrorId : GetContentWriterUnauthorizedAccessError,Microsoft.PowerShell.Commands.AddContentCommand



main{
crearFichero
escribirInfo
}
function crearFichero
{
#Expliqueu que fa la funció!!!
[CmdletBinding()]
Param(

)
Begin
{
#RES
}
Process
{
$nombre_sistema = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name
$ip = Test-Connection $nombre_sistema -Count 1 | select IPV4Address
$ip_replace = $ip.IPV4Address.IPAddressToString.Replace(".","_")
$fecha = Get-Date -Format "MM_dd_yyyy"
$ext = ".txt"
$nombre_fichero = [System.String]::Concat($ip_replace, $fecha, $ext)
$crear_fichero= New-Item C:$nombre_fichero -ItemType file
}
}

function escribirInfo
{
#Expliqueu que fa la funció!!!
[CmdletBinding()]
Param(

)
Begin
{
#RES
}
Process
{
gwmi win32_bios | select SerialNumber, Version, Manufacturer | Add-Content C:$nombre_fichero

}
}
}






powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 21:59







Alberto Martín Rodríguez

















asked Jan 18 at 21:49









Alberto Martín RodríguezAlberto Martín Rodríguez

12




12













  • [1] why all the powershell version tags? please, only use the one that fits your setup. [2] please show the code that triggered the error.

    – Lee_Dailey
    Jan 18 at 21:54













  • Okeey i have already done it

    – Alberto Martín Rodríguez
    Jan 18 at 22:00











  • you have several problems in your code --- [1] the main {} structure is NOT used in powershell [2] your functions need to be defined BEFORE they are called. you have that reversed. [3] variables defined in a function are local to that function. your line 41 is attempting to write to C: - not to the file in the root ... to the root itself. WHY? because $nombre_fichero does not exist in the 2nd function - it was defined in the 1st function.

    – Lee_Dailey
    Jan 18 at 22:07













  • I'm a beginner i do not know hoy to do it

    – Alberto Martín Rodríguez
    Jan 18 at 22:24











  • start with just one thing - one very simple thing. make it work. add another thing. make them work with each other. don't use function util you have the items working with each other ... and even then, use functions only if they are needed.

    – Lee_Dailey
    Jan 18 at 22:33



















  • [1] why all the powershell version tags? please, only use the one that fits your setup. [2] please show the code that triggered the error.

    – Lee_Dailey
    Jan 18 at 21:54













  • Okeey i have already done it

    – Alberto Martín Rodríguez
    Jan 18 at 22:00











  • you have several problems in your code --- [1] the main {} structure is NOT used in powershell [2] your functions need to be defined BEFORE they are called. you have that reversed. [3] variables defined in a function are local to that function. your line 41 is attempting to write to C: - not to the file in the root ... to the root itself. WHY? because $nombre_fichero does not exist in the 2nd function - it was defined in the 1st function.

    – Lee_Dailey
    Jan 18 at 22:07













  • I'm a beginner i do not know hoy to do it

    – Alberto Martín Rodríguez
    Jan 18 at 22:24











  • start with just one thing - one very simple thing. make it work. add another thing. make them work with each other. don't use function util you have the items working with each other ... and even then, use functions only if they are needed.

    – Lee_Dailey
    Jan 18 at 22:33

















[1] why all the powershell version tags? please, only use the one that fits your setup. [2] please show the code that triggered the error.

– Lee_Dailey
Jan 18 at 21:54







[1] why all the powershell version tags? please, only use the one that fits your setup. [2] please show the code that triggered the error.

– Lee_Dailey
Jan 18 at 21:54















Okeey i have already done it

– Alberto Martín Rodríguez
Jan 18 at 22:00





Okeey i have already done it

– Alberto Martín Rodríguez
Jan 18 at 22:00













you have several problems in your code --- [1] the main {} structure is NOT used in powershell [2] your functions need to be defined BEFORE they are called. you have that reversed. [3] variables defined in a function are local to that function. your line 41 is attempting to write to C: - not to the file in the root ... to the root itself. WHY? because $nombre_fichero does not exist in the 2nd function - it was defined in the 1st function.

– Lee_Dailey
Jan 18 at 22:07







you have several problems in your code --- [1] the main {} structure is NOT used in powershell [2] your functions need to be defined BEFORE they are called. you have that reversed. [3] variables defined in a function are local to that function. your line 41 is attempting to write to C: - not to the file in the root ... to the root itself. WHY? because $nombre_fichero does not exist in the 2nd function - it was defined in the 1st function.

– Lee_Dailey
Jan 18 at 22:07















I'm a beginner i do not know hoy to do it

– Alberto Martín Rodríguez
Jan 18 at 22:24





I'm a beginner i do not know hoy to do it

– Alberto Martín Rodríguez
Jan 18 at 22:24













start with just one thing - one very simple thing. make it work. add another thing. make them work with each other. don't use function util you have the items working with each other ... and even then, use functions only if they are needed.

– Lee_Dailey
Jan 18 at 22:33





start with just one thing - one very simple thing. make it work. add another thing. make them work with each other. don't use function util you have the items working with each other ... and even then, use functions only if they are needed.

– Lee_Dailey
Jan 18 at 22:33












2 Answers
2






active

oldest

votes


















1














Users have no permission to create files in the root of C: by default:



PS C:> (get-acl c:).Access


FileSystemRights : AppendData
AccessControlType : Allow
IdentityReference : NT AUTHORITYAuthenticated Users
IsInherited : False
InheritanceFlags : None
PropagationFlags : None

FileSystemRights : -536805376
AccessControlType : Allow
IdentityReference : NT AUTHORITYAuthenticated Users
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : InheritOnly

FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : NT AUTHORITYSYSTEM
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None

FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : BUILTINAdministrators
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None

FileSystemRights : ReadAndExecute, Synchronize
AccessControlType : Allow
IdentityReference : BUILTINUsers
IsInherited : False
InheritanceFlags : ContainerInherit, ObjectInherit
PropagationFlags : None





share|improve this answer































    0














    Welcome Alberto Martín Rodríguez,



    @Gert-Jan-Kraaijeveld is right. You can run as administrator or even better save files to somewhere in your user directory or another suitable location instead of the root of your primary drive.



    i.e. C:$nombre_fichero --> C:Users{{YourUserName}}$nombre_fichero






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54261883%2fadd-content-access-to-the-path-c-is-denied%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Users have no permission to create files in the root of C: by default:



      PS C:> (get-acl c:).Access


      FileSystemRights : AppendData
      AccessControlType : Allow
      IdentityReference : NT AUTHORITYAuthenticated Users
      IsInherited : False
      InheritanceFlags : None
      PropagationFlags : None

      FileSystemRights : -536805376
      AccessControlType : Allow
      IdentityReference : NT AUTHORITYAuthenticated Users
      IsInherited : False
      InheritanceFlags : ContainerInherit, ObjectInherit
      PropagationFlags : InheritOnly

      FileSystemRights : FullControl
      AccessControlType : Allow
      IdentityReference : NT AUTHORITYSYSTEM
      IsInherited : False
      InheritanceFlags : ContainerInherit, ObjectInherit
      PropagationFlags : None

      FileSystemRights : FullControl
      AccessControlType : Allow
      IdentityReference : BUILTINAdministrators
      IsInherited : False
      InheritanceFlags : ContainerInherit, ObjectInherit
      PropagationFlags : None

      FileSystemRights : ReadAndExecute, Synchronize
      AccessControlType : Allow
      IdentityReference : BUILTINUsers
      IsInherited : False
      InheritanceFlags : ContainerInherit, ObjectInherit
      PropagationFlags : None





      share|improve this answer




























        1














        Users have no permission to create files in the root of C: by default:



        PS C:> (get-acl c:).Access


        FileSystemRights : AppendData
        AccessControlType : Allow
        IdentityReference : NT AUTHORITYAuthenticated Users
        IsInherited : False
        InheritanceFlags : None
        PropagationFlags : None

        FileSystemRights : -536805376
        AccessControlType : Allow
        IdentityReference : NT AUTHORITYAuthenticated Users
        IsInherited : False
        InheritanceFlags : ContainerInherit, ObjectInherit
        PropagationFlags : InheritOnly

        FileSystemRights : FullControl
        AccessControlType : Allow
        IdentityReference : NT AUTHORITYSYSTEM
        IsInherited : False
        InheritanceFlags : ContainerInherit, ObjectInherit
        PropagationFlags : None

        FileSystemRights : FullControl
        AccessControlType : Allow
        IdentityReference : BUILTINAdministrators
        IsInherited : False
        InheritanceFlags : ContainerInherit, ObjectInherit
        PropagationFlags : None

        FileSystemRights : ReadAndExecute, Synchronize
        AccessControlType : Allow
        IdentityReference : BUILTINUsers
        IsInherited : False
        InheritanceFlags : ContainerInherit, ObjectInherit
        PropagationFlags : None





        share|improve this answer


























          1












          1








          1







          Users have no permission to create files in the root of C: by default:



          PS C:> (get-acl c:).Access


          FileSystemRights : AppendData
          AccessControlType : Allow
          IdentityReference : NT AUTHORITYAuthenticated Users
          IsInherited : False
          InheritanceFlags : None
          PropagationFlags : None

          FileSystemRights : -536805376
          AccessControlType : Allow
          IdentityReference : NT AUTHORITYAuthenticated Users
          IsInherited : False
          InheritanceFlags : ContainerInherit, ObjectInherit
          PropagationFlags : InheritOnly

          FileSystemRights : FullControl
          AccessControlType : Allow
          IdentityReference : NT AUTHORITYSYSTEM
          IsInherited : False
          InheritanceFlags : ContainerInherit, ObjectInherit
          PropagationFlags : None

          FileSystemRights : FullControl
          AccessControlType : Allow
          IdentityReference : BUILTINAdministrators
          IsInherited : False
          InheritanceFlags : ContainerInherit, ObjectInherit
          PropagationFlags : None

          FileSystemRights : ReadAndExecute, Synchronize
          AccessControlType : Allow
          IdentityReference : BUILTINUsers
          IsInherited : False
          InheritanceFlags : ContainerInherit, ObjectInherit
          PropagationFlags : None





          share|improve this answer













          Users have no permission to create files in the root of C: by default:



          PS C:> (get-acl c:).Access


          FileSystemRights : AppendData
          AccessControlType : Allow
          IdentityReference : NT AUTHORITYAuthenticated Users
          IsInherited : False
          InheritanceFlags : None
          PropagationFlags : None

          FileSystemRights : -536805376
          AccessControlType : Allow
          IdentityReference : NT AUTHORITYAuthenticated Users
          IsInherited : False
          InheritanceFlags : ContainerInherit, ObjectInherit
          PropagationFlags : InheritOnly

          FileSystemRights : FullControl
          AccessControlType : Allow
          IdentityReference : NT AUTHORITYSYSTEM
          IsInherited : False
          InheritanceFlags : ContainerInherit, ObjectInherit
          PropagationFlags : None

          FileSystemRights : FullControl
          AccessControlType : Allow
          IdentityReference : BUILTINAdministrators
          IsInherited : False
          InheritanceFlags : ContainerInherit, ObjectInherit
          PropagationFlags : None

          FileSystemRights : ReadAndExecute, Synchronize
          AccessControlType : Allow
          IdentityReference : BUILTINUsers
          IsInherited : False
          InheritanceFlags : ContainerInherit, ObjectInherit
          PropagationFlags : None






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 18 at 22:02









          Gert Jan KraaijeveldGert Jan Kraaijeveld

          53016




          53016

























              0














              Welcome Alberto Martín Rodríguez,



              @Gert-Jan-Kraaijeveld is right. You can run as administrator or even better save files to somewhere in your user directory or another suitable location instead of the root of your primary drive.



              i.e. C:$nombre_fichero --> C:Users{{YourUserName}}$nombre_fichero






              share|improve this answer




























                0














                Welcome Alberto Martín Rodríguez,



                @Gert-Jan-Kraaijeveld is right. You can run as administrator or even better save files to somewhere in your user directory or another suitable location instead of the root of your primary drive.



                i.e. C:$nombre_fichero --> C:Users{{YourUserName}}$nombre_fichero






                share|improve this answer


























                  0












                  0








                  0







                  Welcome Alberto Martín Rodríguez,



                  @Gert-Jan-Kraaijeveld is right. You can run as administrator or even better save files to somewhere in your user directory or another suitable location instead of the root of your primary drive.



                  i.e. C:$nombre_fichero --> C:Users{{YourUserName}}$nombre_fichero






                  share|improve this answer













                  Welcome Alberto Martín Rodríguez,



                  @Gert-Jan-Kraaijeveld is right. You can run as administrator or even better save files to somewhere in your user directory or another suitable location instead of the root of your primary drive.



                  i.e. C:$nombre_fichero --> C:Users{{YourUserName}}$nombre_fichero







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 18 at 22:09









                  Craig.CCraig.C

                  12919




                  12919






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54261883%2fadd-content-access-to-the-path-c-is-denied%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Liquibase includeAll doesn't find base path

                      How to use setInterval in EJS file?

                      Petrus Granier-Deferre